100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

SOSL Searches

Learn Salesforce Object Search Language (SOSL) for full-text search across multiple objects and fields at once, and when to reach for it instead of SOQL.

Control Flow & CollectionsIntermediate8 min readJul 10, 2026
Analogies

What SOSL does that SOQL can't

SOSL (Salesforce Object Search Language) performs full-text search across multiple objects and multiple fields simultaneously in a single query, whereas SOQL always targets one primary object at a time. A SOSL query like FIND 'Acme' IN ALL FIELDS RETURNING Account(Id, Name), Contact(Id, LastName), Lead(Id, Company) searches the search index (not the raw table) for the term 'Acme' across Accounts, Contacts, and Leads at once, returning up to 2,000 total records across all returned sObject types.

🏏

Cricket analogy: A commentator's archive search for 'Kohli' pulling up match reports, player interviews, and highlight-reel titles all at once — across totally different content types — is exactly what SOSL does across Account, Contact, and Lead simultaneously.

apex
String searchTerm = 'Acme';

List<List<SObject>> searchResults = [
    FIND :searchTerm IN ALL FIELDS
    RETURNING
        Account(Id, Name, Industry WHERE Industry = 'Technology'),
        Contact(Id, FirstName, LastName, Email),
        Lead(Id, Company, Status)
    LIMIT 200
];

List<Account> foundAccounts = (List<Account>) searchResults[0];
List<Contact> foundContacts = (List<Contact>) searchResults[1];
List<Lead> foundLeads = (List<Lead>) searchResults[2];

System.debug('Matched ' + foundAccounts.size() + ' accounts');

IN clauses, wildcards, and result structure

SOSL lets you scope the search field set with IN NAME FIELDS, IN EMAIL FIELDS, IN PHONE FIELDS, or IN ALL FIELDS, and supports wildcards — an asterisk (*) for zero-or-more characters and a question mark (?) for exactly one character — inside the search term, e.g. FIND 'John*' to match Johnson or Johnston. Crucially, the query returns a List<List<SObject>>, a list of lists where each inner list corresponds to one of the sObject types named in RETURNING, in the same order they were listed, which trips up beginners who expect a flat result.

🏏

Cricket analogy: A stats database search scoped 'search only player names' versus 'search everything including commentary transcripts' mirrors choosing IN NAME FIELDS versus IN ALL FIELDS in SOSL.

SOSL is significantly better than SOQL LIKE '%term%' for text search because it uses Salesforce's dedicated search index, which supports relevance ranking and tokenized matching, whereas a SOQL LIKE query with a leading wildcard forces a slow, non-indexed scan across the field on every matching object.

SOSL is the right tool specifically for open-ended, user-typed search boxes — like a global 'search everything' bar — while SOQL remains correct for precise, structured, known-field queries such as WHERE AccountId = :id. A common architectural pattern combines both: use SOSL first to find candidate record Ids matching a loose text search, then run a targeted SOQL query with WHERE Id IN :candidateIds to fetch additional related fields not exposed conveniently through SOSL's RETURNING clause.

🏏

Cricket analogy: A broadcaster first does a loose search across all commentary archives for 'hat-trick' (SOSL-like), then pulls the exact scorecards for just those matching matches (SOQL-like) to get precise stats.

SOSL results are limited to 2,000 records total across all returned sObject types by default (raise-able per-object with a LIMIT clause inside RETURNING, up to that same ceiling), and it can take a few minutes for newly inserted or updated records to appear in the search index, so a record just inserted in the same transaction will not be found by a SOSL search run immediately afterward.

  • SOSL performs full-text search across multiple objects and fields in a single query; SOQL always targets one primary object.
  • SOSL syntax starts with FIND 'term' IN <field-scope> FIELDS RETURNING Object1(fields), Object2(fields), ...
  • Field scope options are IN ALL FIELDS, IN NAME FIELDS, IN EMAIL FIELDS, and IN PHONE FIELDS.
  • Wildcards: * matches zero or more characters, ? matches exactly one character.
  • SOSL queries return a List<List<SObject>>, with one inner list per object type listed in RETURNING, in the same order.
  • SOSL uses the search index for fast, relevance-ranked matching, unlike a non-indexed SOQL LIKE '%term%' scan.
  • SOSL results are capped at 2,000 total records, and newly inserted records may take a few minutes to appear in the search index.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#ApexSalesforceStudyNotes#SOSLSearches#SOSL#Searches#Does#SOQL#StudyNotes#SkillVeris#ExamPrep