Returning
Construct sub-query with provided API.
Methods
The following are methods for Returning
.
with(SObjectField field)
with(SObjectField field1, SObjectField field2)
with(SObjectField field1, SObjectField field2, SObjectField field3)
with(SObjectField field1, SObjectField field2, SObjectField field3, SObjectField field4)
with(SObjectField field1, SObjectField field2, SObjectField field3, SObjectField field4, SObjectField field5)
with(List<SObjectField> fields)
orderBy(String field)
orderBy(String field, String direction)
orderBy(SObjectField field)
orderBy(String relationshipName, SObjectField field)
sortDesc()
nullsLast()
RETURNING
TBD
FIELDS
with field1 - field5
Signature
IReturning with(SObjectField field);
IReturning with(SObjectField field1, SObjectField field2);
IReturning with(SObjectField field1, SObjectField field2, SObjectField field3);
IReturning with(SObjectField field1, SObjectField field2, SObjectField field3, SObjectField field4);
IReturning with(SObjectField field1, SObjectField field2, SObjectField field3, SObjectField field4, SObjectField field5);
Example
FIND 'MySearch'
IN ALL FIELDS
RETURNING Account(Id, Name)
SOSL.find('MySearch')
.inAllFields()
.returning(
SOQL.Returning(Account.SObjectType)
.with(Account.Id, Account.Name)
)
.toSearchList();
with fields
Use for more than 5 fields.
Signature
IReturning with(List<SObjectField> fields);
Example
SELECT Id, (
SELECT Id, Name, Phone, RecordTypeId, Title, Salutation
FROM Contacts
) FROM Account
SOSL.of(Account.SObjectType)
.with(SOSL.SubQuery.of('Contacts')
.with(new List<SObjectField>{
Contact.Id,
Contact.Name,
Contact.Phone,
Contact.RecordTypeId,
Contact.Title,
Contact.Salutation
})
)
.toList();
Example
FIND 'MySearch'
IN ALL FIELDS
RETURNING Contact(Id, Name, Phone, RecordTypeId, Title, Salutation)
SOSL.find('MySearch')
.inAllFields()
.returning(
SOQL.Returning(Contact.SObjectType)
.with(new List<SObjectField>{
Contact.Id,
Contact.Name,
Contact.Phone,
Contact.RecordTypeId,
Contact.Title,
Contact.Salutation
})
)
.toSearchList();
WHERE
whereAre
For more details check SOSL.FilterGroup
and SOSL.Filter
Signature
IReturning whereAre(FilterClause conditions);
Example
SELECT Id, (
SELECT Id
FROM Contacts
WHERE Id = :contactId OR Name = '%John%'
) FROM Account
SOSL.of(Account.SObjectType)
.with(SOSL.SubQuery.of('Contacts')
.whereAre(SOSL.FilterGroup
.add(SOSL.Filter.with(Contact.Id).equal(contactId))
.add(SOSL.Filter.with(Contact.Name).contains('John'))
.conditionLogic('1 OR 2')
)
)
.toList();
FIND 'MySearch'
IN ALL FIELDS
RETURNING Contact(
Id, Name
WHERE Id = 'contactId' OR Name = 'John'
)
SOSL.find('MySearch')
.inAllFields()
.returning(
SOQL.Returning(Contact.SObjectType)
.with(Contact.Id, Contact.Name)
.whereAre(SOSL.FilterGroup
.add(SOSL.Filter.with(Contact.Id).equal(contactId))
.add(SOSL.Filter.with(Contact.Name).contains('John'))
.conditionLogic('1 OR 2')
)
)
.toSearchList();
ORDER BY
order by
Signature
IReturning orderBy(SObjectField field);
Example
SELECT Id, (
SELECT Id
FROM Contacts
ORDER BY Name
) FROM Account
SOSL.of(Account.SObjectType)
.with(SOSL.SubQuery.of('Contacts')
.orderBy(Contact.Name)
)
.toList();
FIND 'MySearch'
IN ALL FIELDS
RETURNING Contact(
Id, Name
ORDER BY Name
)
SOSL.find('MySearch')
.inAllFields()
.returning(
SOQL.Returning(Contact.SObjectType)
.with(Contact.Id, Contact.Name)
.orderBy(Contact.Name)
)
.toSearchList();
orderBy related
Order SOSL query by parent field.
Signature
IReturning orderBy(String relationshipName, SObjectField field);
Example
SELECT Id, (
SELECT Id
FROM Contacts
ORDER BY CreatedBy.Name
) FROM Account
SOSL.of(Account.SObjectType)
.with(SOSL.SubQuery.of('Contacts')
.orderBy('CreatedBy', User.Name)
)
.toList();
FIND 'MySearch'
IN ALL FIELDS
RETURNING Contact(
Id, Name
ORDER BY CreatedBy.Name
)
SOSL.find('MySearch')
.inAllFields()
.returning(
SOQL.Returning(Contact.SObjectType)
.with(Contact.Id, Contact.Name)
.orderBy('CreatedBy', User.Name)
)
.toSearchList();
sortDesc
Default order is ascending (ASC
).
Signature
IReturning sortDesc();
Example
FIND 'MySearch'
IN ALL FIELDS
RETURNING Contact(
Id, Name
ORDER BY Name DESC
)
SOSL.find('MySearch')
.inAllFields()
.returning(
SOQL.Returning(Contact.SObjectType)
.with(Contact.Id, Contact.Name)
.orderBy(Contact.Name).sortDesc()
)
.toSearchList();
nullsLast
By default, null values are sorted first (NULLS FIRST
).
Signature
IReturning nullsLast();
Example
FIND 'MySearch'
IN ALL FIELDS
RETURNING Contact(
Id, Name
ORDER BY Name ASC NULLS LAST
)
SOSL.find('MySearch')
.inAllFields()
.returning(
SOQL.Returning(Contact.SObjectType)
.with(Contact.Id, Contact.Name)
.orderBy(Contact.Name).nullsLast()
)
.toSearchList();
LIMIT
setLimit
Signature
IReturning setLimit(Integer amount);
FIND 'MySearch'
IN ALL FIELDS
RETURNING Contact(
Id, Name
LIMIT 100
)
SOSL.find('MySearch')
.inAllFields()
.returning(
SOQL.Returning(Contact.SObjectType)
.with(Contact.Id, Contact.Name)
.setLimit(100)
)
.toSearchList();
OFFSET
Signature
IReturning offset(Integer startingRow);
Example
FIND 'MySearch'
IN ALL FIELDS
RETURNING Contact(
Id, Name
OFFSET 10
)
SOSL.find('MySearch')
.inAllFields()
.returning(
SOQL.Returning(Contact.SObjectType)
.with(Contact.Id, Contact.Name)
.offset(10)
)
.toSearchList();