Write SOSL Queries

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. SOSL is similar to Apache Lucene.

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

`List<List<SObject>> searchList = [FIND **'SFDC'** IN ALL FIELDS RETURNING **Account**(Name), **Contact**(FirstName,LastName)];`

Differences and Similarities Between SOQL and SOSL

Like SOQL, SOSL allows you to search your organization’s records for specific information. Unlike SOQL, which can only query one standard or custom object at a time, a single SOSL query can search all objects.

Another difference is that SOSL matches fields based on a word match while SOQL performs an exact match by default (when not using wildcards). For example, searching for 'Digital' in SOSL returns records whose field values are 'Digital' or 'The Digital Company', but SOQL returns only records with field values of 'Digital'.

SOQL and SOSL are two separate languages with different syntax. Each language has a distinct use case:

Basic SOSL Syntax

SOSL allows you to specify the following search criteria:

This is the syntax of a basic SOSL query:

FIND **'SearchQuery'** [IN SearchGroup] [RETURNING ObjectsAndFields]