Every search operation follows a strict entry point: SOSL queries begin with the FIND clause. This keyword tells the platform to start a text-based lookup rather than a standard record query.
FIND {'Acme*'}
IN ALL FIELDS
Use SOSL to write text based search queries in your org based on the search index. This is distinct from SOQL as it scans indexed text data for lightning-fast matching across massive datasets.
Speed: Faster for large text lookups
With SOSL, the search engine looks for matches to the search term requested across a maximum of 2000 records. So, ensure the filters are very selective while searching and using SOSL.
SOSL comes in handy when you need to search for any key identifier in an object among multiple objects—be it related or not.
Use SOSL when you need to search for something across the entire org, scanning multiple objects simultaneously.
SOSL is optimized to search Phone, Email, and Text type fields only (PET) for maximum efficiency.
When you do not know the object that has the result set, use SOSL to find the correct data source.
Explore how to harness global search to find data across your entire Salesforce organization.
Question: How do you find the word {Acme} across both Accounts and Contacts simultaneously?
Pro Tip: Use the RETURNING clause to specify which fields you want back from each object.
// The Solution
FIND {Acme*}
IN ALL FIELDS
RETURNING Account(Name), Contact(FirstName, LastName)
Question: How do you search for any Lead or User whose name or email starts with 'test'?
Wildcard Rule: The * symbol at the end of a word allows for partial matching in SOSL.
// The Solution
FIND {test*}
IN NAME FIELDS
RETURNING Lead(Name, Email), User(Name)
Question: How do you find every record containing the keyword 'Salesforce' across your primary business objects?
Search Logic: By returning multiple objects, you create a single result set that acts like a global search bar within your custom Apex logic.
// The Solution
FIND {Salesforce*}
IN ALL FIELDS
RETURNING Account(Name), Contact(Name), Lead(Company), Opportunity(Name)
Question: How do you search all records with the specific name "Snap Tales"?
Scoping Rule: Since the request specifies searching by "name," using the NAME FIELDS scope makes the search faster and more selective than scanning every field in the org.
// The Solution
FIND {Snap Tales}
IN NAME FIELDS
RETURNING Account(Name), Contact(Name), Lead(Name)
Question: How do you find all Contacts with the name 'Himanshu' and display their name and phone information?
PET Field Logic: Since you are searching for a name and retrieving phone data, SOSL is highly efficient as it specifically indexes these Phone, Email, and Text (PET) fields.
// The Solution
FIND {Ram}
IN NAME FIELDS
RETURNING Contact(Name, Phone)
Question: How do you find all Leads named either "Betty" or "David" that have been modified in the current month?
Filtering Rule: While FIND handles the keywords, you can use a WHERE clause inside the object parentheses to filter by specific field values like dates.
// The Solution
FIND {Betty OR David}
IN NAME FIELDS
RETURNING Lead(Name, LastModifiedDate WHERE LastModifiedDate = THIS_MONTH)