Salesforce Object Search Language (SOSL) is a powerful tool for performing text-based searches across multiple objects and fields within Salesforce. Unlike SOQL, which is used to query records from a specific object or related objects, SOSL is designed for searching across multiple objects or fields in a single search operation. It is especially useful when you don’t know exactly where the data you’re looking for is stored but want to search for keywords across multiple objects, such as Account, Contact, Opportunity, etc.
Because SOSL queries are often used for broader search scenarios (e.g., full-text search or global search), it's important to follow best practices to ensure they are efficient, effective, and maintainable.
Here are some SOSL best practices to help you get the most out of your searches in Salesforce:
Specify the Objects to Search: Always specify the objects you want to search to limit the scope of the query. The more specific you are about the objects, the faster your query will run, as it narrows down the search space.
sql
FIND {searchTerm} IN ALL FIELDS RETURNING Account(Id, Name), Contact(Id, FirstName, LastName)
This example restricts the search to Account and Contact objects, making it more efficient.
Use RETURNING to Limit Fields: SOSL allows you to search across multiple fields, but it’s important to only return the fields that are necessary for your use case. The more fields you return, the more data the query will retrieve, which can negatively impact performance.
sql
FIND {searchTerm} IN NAME FIELDS RETURNING Account(Id, Name), Contact(Id, FirstName, LastName)
Avoid returning all fields unnecessarily, such as:
sql
FIND {searchTerm} IN ALL FIELDS RETURNING Account(Id, Name, BillingAddress, Phone, Website, Industry)
Avoid Overuse of Wildcards (*): Using wildcards in SOSL queries can sometimes result in performance issues, especially when used at the beginning of a search term (e.g., *term), as it leads to a more expensive full-text search. It's better to use wildcards at the end of the term to narrow down the results more efficiently.
sql
FIND {Acme*} IN NAME FIELDS RETURNING Account(Id, Name)
Avoid Leading Wildcards: As with SOQL, avoid using a leading wildcard (*term) because Salesforce cannot take full advantage of its indexing capabilities.
sql
FIND {Acme*} IN NAME FIELDS RETURNING Account(Id, Name)
Instead of:
sql
FIND {*Acme} IN NAME FIELDS RETURNING Account(Id, Name)
This ensures the search is more efficient and faster.
Use LIMIT: SOSL queries can return many records, but it’s often best to limit the number of results you retrieve, especially if you're dealing with large datasets. Always use the LIMIT keyword to restrict the number of records returned by the search.
sql
FIND {searchTerm} IN NAME FIELDS RETURNING Account(Id, Name LIMIT 10), Contact(Id, FirstName, LastName LIMIT 10)
Return a Small Number of Results: In most cases, users don't need to see all the records that match a search term, so restricting the number of records returned (e.g., LIMIT 10) can improve both performance and usability.
Minimize the Number of Objects and Fields Searched: Always try to narrow the search to only the relevant objects and fields. Searching across all fields of multiple objects (IN ALL FIELDS) may return large result sets, causing unnecessary processing and performance degradation.
sql
FIND {searchTerm} IN NAME FIELDS RETURNING Account(Id, Name), Contact(Id, FirstName, LastName)
Instead of:
sql
FIND {searchTerm} IN ALL FIELDS RETURNING Account(Id, Name), Contact(Id, FirstName, LastName)
Use Indexed Fields: SOSL is optimized to search fields that are indexed, like Name or Email on standard objects. Whenever possible, target these fields for more efficient searches.
Use SOSL for Text-Based Searches: SOSL is best suited for full-text or free-text searches, where you need to search for keywords across multiple fields and objects. If you need to query records based on specific field values or relationships, use SOQL instead.
sql
FIND {searchTerm} IN ALL FIELDS RETURNING Account(Id, Name), Contact(Id, FirstName, LastName)
If you're searching for a specific field or related record, SOQL is often a better choice:
sql
SELECT Name FROM Account WHERE Industry = 'Technology'
Use Specific Search Fields: If you're conducting searches on a known field, such as Name, Email, or Phone, specify the field to search against. Avoid searching in all fields unless absolutely necessary, as searching in all fields can result in slower performance and irrelevant results.
sql
FIND {searchTerm} IN NAME FIELDS RETURNING Account(Id, Name), Contact(Id, FirstName, LastName)
Search in Specific Field Types: You can also specify which field types to search using IN ALL FIELDS, IN NAME FIELDS, or IN EMAIL FIELDS. If you only need to search by Name, avoid including irrelevant fields in the search scope.
Process Results Efficiently: When processing SOSL results, be mindful of the structure of the data returned. SOSL queries return a list of lists (each list corresponding to an object type). Loop through these lists to process the results efficiently.
javascript
const result = searchResult;
if (result && result.length > 0) {
result.forEach(group => {
group.forEach(record => {
// Process each record here
});
});
}
Handle Null or Empty Results: Be sure to check for empty results or errors from the SOSL query before processing the data to avoid running into runtime exceptions.
javascript
if (searchResult && searchResult.length > 0) {
// Process the results
} else {
// Handle empty or null result set
}
Be Aware of SOSL Governor Limits: Salesforce enforces governor limits on SOSL queries, such as the maximum number of records returned, the maximum number of queries in a single transaction, etc. Always keep these limits in mind when designing your solution, especially when performing multiple SOSL queries within a loop or large batch processing tasks.
Consider Pagination: If your SOSL query is likely to return a large result set, consider implementing pagination to retrieve results in smaller chunks. This can prevent hitting governor limits and improve the user experience.
SOSL in Apex: When calling SOSL from Apex, ensure you use the correct Apex syntax and handle results carefully. Apex allows you to use SOSL in search() or via the Database.query() method.
Example of an Apex SOSL query:
java
List