Skip to main content
Development

What is SOSL in Salesforce?

Updated · By the Clientell AI team · How we write

SOSL (Salesforce Object Search Language) is the Salesforce language for searching text across many objects and fields in one call. You give it a word or phrase and a list of objects. It returns a list of records for each object. Use it when you do not know which object or field holds the value.

Key facts

SOSL full form
Salesforce Object Search Language
What it does
Full-text search across many objects and fields at once
Returns
A list of lists, one list of records per object you name
Apex limits
20 SOSL queries per transaction, 2,000 records per query (Apex Developer Guide)
Main clause
FIND, with IN for the field scope and RETURNING for the objects (SOQL and SOSL Reference)
Needs an index
Yes. It searches the search index, so brand-new records can take a short time to appear.

Clientell AI, the AI Salesforce admin, helps with SOSL in a sandbox: it writes and tests the code, and shows it to you before anything deploys. See Apex and query generation

Try it in Clientell AI

“Write a SOSL example for my Account object, test it in a sandbox, and show me the code before anything deploys.”

Clientell AI reads your org first. Platform deployments include rollback. How security works.

Start the 14-day free trial, no card

What does SOSL mean?

SOSL means Salesforce Object Search Language. It is the text-search language of the platform. You give it a word or phrase, say which fields to look in, and name the objects to return. Think of a search box that a developer can call from code (SOQL and SOSL Reference).

The two differ. SOQL. SOQL filters records in one object by field values. SOSL looks for text in many objects at once. If a user types "Acme" and you want every Account, Contact, Lead and Opportunity that mentions it, SOSL is the right tool.

How does SOSL syntax work? Example with FIND

A SOSL query starts with FIND and the search text. Then comes IN for the field scope and RETURNING for the objects and fields you want. In Apex the search text sits in single quotes. In the API it sits in braces. The wildcards are * for any number of characters and ? for one (FIND reference).

This Apex searches four objects for text that starts with Acme. The result is a list of lists, so the first list holds Accounts, the second Contacts, and so on in RETURNING order.

  • IN ALL FIELDS searches every searchable text field. IN NAME FIELDS, IN EMAIL FIELDS, IN PHONE FIELDS and IN SIDEBAR FIELDS narrow the scope.
  • RETURNING lists the objects. Add a field list and an optional WHERE, ORDER BY or LIMIT for each object.
  • The Developer Console Query Editor and the REST API wrap the search text in curly braces instead of single quotes.
  • Optional clauses include WITH DIVISION, WITH NETWORK and WITH SNIPPET.
List<List<SObject>> results = [
  FIND 'Acme*' IN ALL FIELDS
  RETURNING
    Account(Id, Name WHERE Industry = 'Banking'),
    Contact(Id, FirstName, LastName, Email),
    Lead(Id, Name, Company),
    Opportunity(Id, Name, Amount)
  LIMIT 20
];

List<Account> accounts = (List<Account>) results[0];
List<Contact> contacts = (List<Contact>) results[1];

What is the difference between SOSL and SOQL?

Pick by what you already know. Know the object and the field? Write a query. Know only some text? Run a search.

SOQLSOSL
Best forFiltering records by field valuesFinding text across the org
Objects per callOne object plus its relationsMany objects
Search styleExact and pattern match on fieldsWord match through the search index
ReturnsA list of recordsA list of lists of records
Apex queries per transaction100 sync, 200 async (Apex Developer Guide)20 (Apex Developer Guide)
Records per transaction50,000 across SOQL (Apex Developer Guide)2,000 per SOSL query (Apex Developer Guide)
GROUP BY and aggregatesYesNo

What are the SOSL limits?

Each Apex transaction can issue 20 SOSL queries, and one SOSL query returns at most 2,000 records (Apex Developer Guide). SOQL has its own separate query count. Over the API, a SOSL request also returns at most 2,000 results in total (Developer Limits Quick Reference). See governor limits for the rest.

Example: find a customer by phone number

A support agent has a caller's number but no record. The search below checks every phone field on Contact, Lead and Account. The result holds three lists, and each list may be empty.

String num = '5551234567';
List<List<SObject>> found = [
  FIND :num IN PHONE FIELDS
  RETURNING
    Contact(Id, Name, Account.Name, Phone),
    Lead(Id, Name, Company, Phone),
    Account(Id, Name, Phone)
];
Integer contactHits = found[0].size();
Integer leadHits = found[1].size();

When is SOSL the wrong choice?

This language is the wrong choice when you need a complete and exact list of records, such as for a data migration or a report. It searches an index and returns matches by word, not every row.

  • Newly created records may not appear until the index updates. Fix: use SOQL when you need records you just saved.
  • Very short terms and common words can be ignored by the search engine. Fix: search for a longer, more specific term.
  • You need totals or groups. Fix: SOQL with GROUP BY, since SOSL cannot aggregate.
  • Field security. Apex in system mode ignores field-level security while it scans with IN ALL FIELDS (FIND reference). Fix: add WITH USER_MODE in Apex (see Apex security).

Where can you run SOSL?

Run SOSL in Apex, in the Developer Console Query Editor (the SOSL tab), in Workbench, through the REST API search endpoint and through the SOAP API search call. For field filters on a single object, use SOQL instead.

FAQ: SOSL in Salesforce

What is the full form of SOSL in Salesforce?

SOSL stands for Salesforce Object Search Language. It searches text across many objects and fields in one call.

What is SOSL in Salesforce?

It is a search language that takes a word or phrase and returns matching records from the objects you list. In Apex the result is a list of lists.

What is the difference between SOSL and SOQL?

SOQL filters records in one object by field values. SOSL searches text across many objects at once and returns a list of lists. SOSL cannot group or total data.

How many SOSL queries can Apex run?

20 per transaction, and each query can return up to 2,000 records (Apex Developer Guide).

When should I use SOSL?

Use it when you have some text, like a name, email or phone number, and you do not know which object or field holds it.

Does SOSL return results from every object?

Only from the objects named in the RETURNING clause. Use user mode to limit results to what the running user can see.

Getting Started

Try this on your own org

Start the 14-day free trial, no card. Clientell AI asks before it changes anything in production.

14-day free trial  ·  No credit card required

SOC 2
HIPAA
ISO 27001
Salesforce Partner