How to use SOSL query to search in multiple Salesforce objects?

SOSL is the Salesforce Object Search Language. We can search words, phrases or text with wildcard characters in multiple standard and Custom objects in Salesfore. Unlike ‘SELECT’ command in SOQL, we will use ‘FIND’  command to search in SOSL query.

SOSL statements evaluate to a list of lists of sObjects, where each list contains the search results for a particular sObject type. In other words, a SOSL query returns a List<List>

For e.g. If we are searching on three different objects: Accounts, Contacts, opportunities, the search result list (main list) will contain three separate lists one each for Accounts, Contacts, opportunities. 

The result lists are always returned in the same order as they were specified in the SOSL query.

The following query is searching all Accounts, Contacts, opportunities and leads with the word ‘dell’ in all fields.  ALL FIELDS is the key word we will use to search all fields in the above objects.

List<List> searchList = [FIND 'dell*' 
IN ALL FIELDS RETURNING Account (Id, Name), Contact, Opportunity, Lead];

Account [] accList = ((List)searchList[0]); 
Contact [] conList = ((List)searchList[1]);
Opportunity [] oppsList = ((List)searchList[2]);

The List returned must be typecasted before we use, as the list of returned objects are decided at run time.

If you like to search in Name filed only you need to replace ALL FIELDS with NAME FIELDS command.

List<List> searchList = [FIND 'dell*' 
IN NAME FIELDS RETURNING Account (Id, Name), Contact, Opportunity];

In summary, we can use SOSL Query to do a global search. There is no relationship required between object to be queried.

Dynamic SOSL

If you want to build your SOSL query based on a text filed or a parameter at run time, you can construct the SOSL query in a string and use search.query to execute the SOSL query.

String searchquery='FIND\'dell*\'IN ALL FIELDS RETURNING Account(id,name),Contact, Opportunity'; 


Search highlights and snippets give your agents and users context as to why the particular result matched their search query. The relevant text appears below the title with the search terms in bold. You can enable search highlights and snippets on the Salesforce Knowledge.

Search highlights and snippets are only generated from the following fields:

  • Email
  • Long text area
  • Rich text area
  • Text area

Below example exercises a simple SOSL query string that includes a WITH SNIPPET clause. The example calls System.debug() to print the returned article titles and snippets. Your code would display the titles and snippets in a Web page.

Search.SearchResults searchResults = Search.find('FIND \'test\' IN ALL FIELDS RETURNING KnowledgeArticleVersion(id, title WHERE PublishStatus = \'Online\' AND Language = \'en_US\') WITH SNIPPET (target_length=120)');
List articlelist = searchResults.get('KnowledgeArticleVersion');

for (Search.SearchResult searchResult : articleList) { 
	KnowledgeArticleVersion article = (KnowledgeArticleVersion) searchResult.getSObject(); 


Senior Salesforce Architect
In Connect me on LinkedIn
twitter Follow me on Twitter @apexcoder
blogger Subscribe to my blog
email Email me

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s