In Salesforce, WITH SPELL_CORRECTION and WITH HIGHLIGHT are important SOSL clauses, introduced in Summer ’17, that continue to provide improved search functionality with subsequent Salesforce releases. Below is an enhanced overview of these clauses with the latest information as of API version 58, including key updates to improve search experiences across Salesforce platforms.
1. WITH HIGHLIGHT
The WITH HIGHLIGHT clause allows SOSL queries to return search results with the search terms highlighted, making it easier to identify relevant content, especially for business accounts, campaigns, contacts, leads, opportunities, quotes, users, and custom object searches.
Enhancements in API Version 58:
- Expanded support for Custom Objects: Initially, this clause was available only for certain standard objects, but starting from API version 58, more custom objects and fields are supported.
- Additional supported field types include Rich Text and Formula Fields that evaluate to text.
- This feature is still compatible with Lightning, Classic, and Salesforce Mobile, and can be used in API integrations (SOAP, REST) as well.
Field Types Supporting Highlights:
- Auto Number
- Text
- Text Area
- Text Area (Long)
- Rich Text (New in Version 58)
- Formula Fields with Text (New in Version 58)
Example Query:
FIND {tutorials} IN ALL FIELDS RETURNING Account(Name, Description) WITH HIGHLIGHT
This query returns search results for “tutorials,” and the word “tutorials” will be highlighted in the result fields.
Important Notes:
- Only 25 records per object per SOSL query are highlighted.
- Search terms with wildcards are not highlighted.
- Objects without support for WITH HIGHLIGHT won’t show highlighted terms.
- The clause is supported in SOAP API, REST API, and Apex.
2. WITH SPELL_CORRECTION
The WITH SPELL_CORRECTION clause enables or disables spell correction for a search. When set to true
, it corrects potential misspellings in the search query, similar to how Google corrects search terms.
Updates in API Version 58:
- Improved Language Support: The clause now supports spell correction in more languages, including non-English languages such as French, German, and Spanish, with additional regional variants.
- Dynamic Detection: Salesforce’s search engine now detects spelling errors based on the user’s language settings or the preferred locale of the org, improving accuracy.
Usage:
The default behavior of the clause is true
, meaning that Salesforce automatically applies spell correction unless explicitly disabled.
Example Query:
FIND {your search string} IN ALL FIELDS RETURNING Account WITH SPELL_CORRECTION = false
In this query, spell correction is disabled by setting WITH SPELL_CORRECTION = false
.
Key Updates:
- Multi-Language Spell Correction: Spell correction now works seamlessly with searches performed in multiple languages based on the org’s language settings.
- More Accurate Suggestions: The spell correction engine now uses machine learning to enhance the accuracy of suggested corrections in search results.
Supported APIs:
- Available in SOAP API, REST API, and Apex, making it widely usable across various Salesforce environments.
Best Practices for Using WITH HIGHLIGHT and WITH SPELL_CORRECTION:
- Limitations on Highlighting:
- If you are using wildcards (
*
), the system does not highlight terms with incomplete matches. For example,stutorial*
will not show highlights in results. - Remember to use specific field types for highlighting to work properly (text-based fields like Text, Email, Auto Number).
- Optimizing Search Performance:
- Use the WITH HIGHLIGHT clause in searches where users need immediate visual cues for relevant content, particularly when querying long-text fields or descriptions.
- For environments where users may enter incorrect spellings frequently (e.g., multi-language support or complex terms), enabling WITH SPELL_CORRECTION ensures that misspelled queries still return relevant results.
Additional Resources:
- Full Documentation: Check out the latest Salesforce SOSL and SOQL features in the official Salesforce Documentation.
- API Version 58 Release Notes: Explore more details on what’s new with SOSL clauses in the Salesforce API Version 58 Release Notes.
By leveraging these two powerful clauses, Salesforce developers can build smarter, more intuitive search functionalities that enhance the user experience in both Lightning and Classic environments.