SOSL in Salesforce
Salesforce Object Search Language is a search language of salesforce and the important feature is that Unlike SOQL, we can search in multiple objects at same time using single SOSL. In SOQL, we can query only one object at a time but in SOSL, we can search for some specified string like ‘anyString’ in multiple objects at the same time.
- We can search for some specified string like ‘anyString’ in multiple objects at the same time.
- We can mention in which fields of all the sObjects,we want to search for the specified string.
- The SOSL query start with ‘FIND’ keyword .
- You can also specify, which fields to return for each object mentioned in SOSL query. Suppose you need to perform search on three objects like Account, Contact & Opportunity then you can mention like, for list returned with Account results only (Name, Industry) fields should be returned, and for Contact results (firstName, lastName) should be returned and similarly for Opportunity too.
- The final result of SOSL is a list of lists of sObjects.
- The returned result contains the list of sObjects in the same order as the order you defined in SOSL query.
- If a SOSL query does not return any records/values for a specified sObject, then search results include an empty list for that sObject type.
- The search string should be at least of two characters long.
Here is the Example :
List<list<SObject>> searchList = [FIND ‘map*’ IN ALL FIELDS RETURNING Account (Id, Name), Contact, Opportunity];
Account [] acts = ((List)searchList[0]);
Contact [] cnts = ((List)searchList[1]);
Opportunity [] oppties = ((List)searchList[2]);
Example
FIND {SFDC} IN ALL FIELDS RETURNING Account(Name),Contact(FirstName,LastName) We can use this in apex like this : List<list<SObject>> searchList = [FIND 'SFDC' IN ALL FIELDS RETURNING Account(Name), Contact(FirstName,LastName)]; listaccs = (list ) searchList[0]; list cons = (list ) searchList[1];
This SOSL query looks for the term ‘SFDC’ in the Account’s Name field and the Contact’s FirstName and LastName fields.
NOTE : While using an SOSL query in the editor, you need to enclose the search term in curly braces {<search term>}, but in the case of SOSL in apex you need to enclose the search term in single quotes like this, ‘<search term>’
Running SOSL Queries
Query Editor
While running SOSL queries in the developer console’s query editor, we need to use a single brace with our search term instead of using a single quote (‘) like in apex, so the same SOSL query as the last example will look like this when run in the query editor.
FIND {SFDC} IN ALL FIELDS RETURNING Account(Name), Contact(FirstName,LastName)
Workbench
Go to the workbench tool : https://workbench.developerforce.com/. Login with your salesforce account and then you need to go to Queries -> SOSL Search
Once in the SOSL search tool you need to use the same syntax with the curly braces as you used in the query editor.
You need to enter the query in the search box, click on Search and you should be able to see the results!