What is SOSL in Salesforce?

Asked 30-Jan-2018
Viewed 386 times

1 Answer


0

SOSL stands for “Salesforce Object Search Language”. It is used to construct text-based search queries across the search index. It gives the facility to search text, phone, email etc. for multiple objects, with custom objects included.

For SOSL Query you need to write SELECT queries that use a CONTAINS or FREETEXT statement. The powerful CONTAINS search has many variations, it allows to search for exact or fuzzy matches.

SOSL does not use SOQL SELECT keyword, it uses a simpler search syntax, for ex. SOSL uses FIND keyword instead of SOQL SELECT.

Below is the SOSL’s basic query example:

FIND ‘Pyramid*’ IN ALL FIELDS RETURNING Account(Name),

                                        Contact(LastName, FirstName, Email)

As you can see the above query has three parts:

  • FIND Clause with Search Term:

here FIND clause makes SOSL search unique. Now, FIND is followed by whatever search term you’re looking for, it can be a single word or phrase.

In our case, we are using word “Pyramid” and we are also using a wildcard character (*) in it that is also allowed in the search term.

  • IN Clause:

this is used to specify a search group. IN clause tell to Salesforce that which fields we want to search. In our example, we specified ALL FIELDS.

The literal "ALL FIELDS", means you are specifying to search all the fields of the object that is specified in the RETURNING clause.

If you want to search only name fields then we will use simply field names, for ex.: for phone fields, we use PHONE FIELDS.

  • RETURNING Clause:

The returning clause is used to specify which data you want to return and from which object to be searched.

In our example, we are returning Name field from Account object and the LastName, FirstName from Contact object. The returned result only include those fields that are listed in parenthesis.

If you didn’t specify the field names only specifying the object name, then only the ID of the specified object will be returned if match found.


Below are the following environments from where you can access in a single query:

  • Apex Statements
  • SOAP or REST calls
  • Visualforce controllers and getter methods
  • Schema Explorer of the Eclipse Toolkit