SOSL Basics (Salesforce Object Search Language)
SOSL (Salesforce Object Search Language) is another useful way of creating filters and searching across your Salesforce record data. The SOSL query search analyzes all the records in the index and by default, only returns the top 2,000 matches based on their relevancy. Explore SOSL and its uses in this chapter.
Search analyzes all records in the index; by default, only the top 2,000 matched records are returned based on relevancy. This API supports pagination to fetch additional matches. Sharing is applied after the result set is returned from the search stack. If your filters aren’t selective and cause search term matches of more than 2000 records, there’s a possibility of running into search crowding.
What Is SOSL Query In Salesforce?
SOSL is a highly optimized way of searching Salesforce records across multiple objects specified in the query. A SOSL query returns a 2D list of sObjects and it can be performed on multiple objects. When building efficient SOSL queries, create filters that are selective. If your filters aren’t selective and cause search term matches of more than 2000 records, there’s a possibility of running into search crowding.
When To Use SOSL?
Whether you use SOQL or SOSL depends on whether you know which objects or fields you want to search, plus other considerations.
Use SOSL when you don’t know which object or field the data resides in, and you want to:
- Retrieve data for a specific term that you know exists within a field. Because SOSL can tokenize multiple Field Groups within a field and build a search index from this, SOSL searches are faster and can return more relevant results.
- Retrieve multiple objects and fields efficiently where the objects might or might not be related to one another.
- Retrieve data for a particular division in an organization using the divisions feature.
- Retrieve data that’s in Chinese, Japanese, Korean, or Thai. Morphological tokenization for CJKT terms helps ensure accurate results.
Use Cases of Salesforce Object Search Language (SOSL)
There are various ways in which SOSL can be helpful in your Salesforce org. Look at some of these use cases below:
Search Across Multiple Objects
SOSL Query allows you to search across multiple objects at onece. These objects can be both standard and custom. Such as when you need to retrieve records from different objects like Accounts, Contacts, and Opportunities in a single query.
Example:
FIND {Acme} IN Name FIELDS RETURNING Account(Name), Contact(FirstName, LastName) |
FullText Search
You can search within fields like Notes, Emails, or Custom Fields for keywords and the SOSL query will return you with full text of related records.
Example:
FIND {support} IN ALL FIELDS RETURNING Case(Subject, Description) |
Faster Search with Indexes
SOSL uses indexed searches, making it much faster than SOQL when querying large volumes of records.
Example:
FIND {John} IN SIDEBAR FIELD RETURNING Contact(Id, Email) |
Search with Wildcards
Wildcards in SOSL queries can be used to find records with similar names or partial matches.
For example, searching for names starting with specific letters.
Example:
FIND {Joh*} IN Name FIELDS RETURNING Contact(FirstName, LastName) |
Searching for Records with Null or Empty Values
SOSL can be used to identify records that have empty or missing data, such as records where a specific field like Email or Phone is not populated.
Example:
FIND {} IN Email FIELDS RETURNING Lead(FirstName, LastName) |
Need more support?
Get a head start with our FREE study notes!
Learn more and get all the answers you need at zero cost. Improve your skills using our detailed notes prepared by industry experts to help you excel.