SOSL Queries in Salesforce

Salesforce Object Search Language (SOSL) is a Salesforce search language that is used to perform text searches in records. Use SOSL to search fields across multiple standard and custom object records in Salesforce.

This is an example of a SOSL query that searches for accounts and contacts that have any fields with the word ‘SFDC’.

List searchList = [FIND ‘SFDC’ IN ALL FIELDS                                                                                                         RETURNING Account(Name), Contact(FirstName,LastName)];

SOSL Syntax –

FIND ‘SearchQuery‘ [IN SearchGroup] [RETURNING ObjectsAndFields]

SearchQuery is the text to search for (a single word or a phrase). Search terms can be grouped with logical operators (AND, OR) and parentheses. Also, search terms can include wildcard characters (*, ?). The * wildcard matches zero or more characters at the middle or end of the search term. The ? wildcard matches only one character at the middle or end of the search term.

Text searches are case-insensitive. For example, searching for Customer, customer, or CUSTOMER all return the same results.

SearchGroup is optional. It is the scope of the fields to search. If not specified, the default search scope is all fields. SearchGroup can take one of the following values.

  • ALL FIELDS
  • NAME FIELDS
  • EMAIL FIELDS
  • PHONE FIELDS
  • SIDEBAR FIELDS

ObjectsAndFields is optional. It is the information to return in the search result—a list of one or more sObjects and, within each sObject, list of one or more fields. If not specified, the search results contain the IDs of all objects found.

Using SOSL in Apex


List<List<sObject>> searchList = [FIND 'Wingo OR SFDC' IN ALL FIELDS 
                        RETURNING Account(Name),Contact(FirstName,LastName,Department)];
Account[] searchAccounts = (Account[])searchList[0];
Contact[] searchContacts = (Contact[])searchList[1];

System.debug('Found the following accounts.');
for (Account a : searchAccounts) {
 System.debug(a.Name);
}

System.debug('Found the following contacts.');
for (Contact c : searchContacts) {
 System.debug(c.LastName + ', ' + c.FirstName);
}

This SOSL query combines two search terms by using the OR logical operator—it searches for Wingo or SFDC in any field. This example returns all the sample accounts because they each have a field containing one of the words. The SOSL search results are returned in a list of lists. Each list contains an array of the returned records. In this case, the list has two elements. At index 0, the list contains the array of accounts. At index 1, the list contains the array of contacts.

Using Filter, Reorder and Limit in SOSL 

You can use WHERE clause, ORDER BY, and LIMIT the returned results of a SOSL query. Because SOSL queries can return multiple sObjects, those filters are applied within each sObject inside the RETURNING clause.

For example, this results in only accounts whose industry is Apparel to be returned:     RETURNING Account(Name, Industry WHERE Industry=’Apparel’)

For example this causes the returned accounts to be ordered by the Name field:             RETURNING Account(Name, Industry ORDER BY Name).

This example limits the returned accounts to 10 only:                                                           RETURNING Account(Name, Industry LIMIT 10)

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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