Salesforce Object Search Language (SOSL) in Apex – Complete Guide with Examples (2026)

Salesforce developers often need to search records quickly and efficiently. In real-world projects, sometimes we don’t know exactly which object contains the data we are looking for.

In such cases, Salesforce provides a powerful feature called Salesforce Object Search Language (SOSL). SOSL allows us to search across multiple objects and fields using keywords, making it very useful for building search functionality in applications.

Unlike structured queries, SOSL works like a global search, helping developers find data even when the exact location is unknown.

In this tutorial, we will learn about Salesforce Object Search Language (SOQL) in Apex. I will explain when to use SOSL and how to write effective SOSL queries in Apex to search and retrieve Salesforce data.

What is Salesforce Object Search Language (SOSL)?

Salesforce Object Search Language(SOSL) is used to perform text searches in Salesforce records. Using SOSL, we can search fields across multiple standard and custom objects in Salesforce. It is like a global search box in Salesforce.

A SOSL query returns a list of sObjects, where each list contains the results from the search for a particular sObject type.

A SOSL query always returns results in the same order that was specified in the SOSL query. Whenever an SOSL query returns no records for a specified sObject type, it is returned as an empty list in the search results.

In simple words:

  • SOQL = Structured search (you know where data is)
  • SOSL = Global search (you don’t know where the data is)

Basic SOSL Syntax:

FIND 'Search Text' 
IN Search Group 
RETURNING Object1 ( Fields ), Object2 ( Fields )

Before taking the examples, first, let’s understand the syntax. After that, writing SOQL queries will be very easy.

1. FIND ‘Search Text.’

  • Here, we can specify what text we are searching for, and it can find partial matches.
  • For example, if you search for ‘John,’ it will return records containing ‘John,’ ‘Johnny,’ or ‘Johnson.’

2. IN Search Group

Here, we can specify where Salesforce should search for the text we provide. For that, here are the options we can provide to find text in those fields.

Search GroupDescription
All Fields Searches across all searchable fields in all objects.
Name FieldsSearches only in name fields (e.g., FirstName, LastName).
Email FieldsSearches in email fields.
Phone FieldsSearches in phone number fields.
SideBar FieldsSearches the same fields as Salesforce’s global search.

3. RETURNING Object1(Fields), Object2(Fields)

Here, we can specify which objects and fields should be retrieved from the search.

  • Object1, Object2, … -> The objects to search in (e.g., Contact, Account, Lead).
  • Fields: The specific fields to return.

Why Do We Use SOSL in Apex?

SOSL is very useful in real-time Salesforce applications. It helps developers build powerful search features like:

  • Global search functionality
  • Searching customer records using keywords
  • Finding data across multiple objects
  • Handling fuzzy search (partial match)

In many systems, users don’t remember exact record names. They type partial values, and SOSL helps return relevant results.

Real-world example: A user types “John” → system should return:

  • Contacts with the name John
  • Accounts containing John
  • Leads with John

This is where SOSL is very helpful.

Key Features of SOSL

  • Search across multiple objects in one query
  • Supports wildcard search (* and ?)
  • Works on text-based search
  • Returns data in a grouped format (list of lists)
  • Faster for searching large data

SOSL works similarly to a search engine, helping retrieve relevant records quickly.

Difference Between SOSL and SOQL

FeatureSOSLSOQL
Search TypeText-based searchStructured query
ObjectsMultiple objectsSingle object
Use CaseUnknown data locationKnown data location
PerformanceFaster for searchBetter for filtering
ExampleFIND ‘John’SELECT Name FROM Contact

Example: Returning Records For All Fields Using SOSL

We want to return records that contain ‘And‘ text from all the fields of contact and lead object, but only display the following fields related to a particular object.

  • Contact(Name, Email)
  • Lead(Name, Company)

On the Developer Console -> click the Query Editor -> write the SOSL query, -> click on the Execute button.

FIND {And} IN All FIELDS RETURNING Contact(Name, Email), Lead(Name, Company)
  • In the above SOSL query, search for the ‘And’ text in all searchable fields.
  • Then, it returns matching records from contacts with name and email fields and leads with name and company fields.

Below, if you look at some records, we provided ‘And’ text for searching. However, in the text we provided, we are searching for all fields in the contact and lead objects. So, the searchable text is not in the field we have displayed in some records.

Salesforce Object Search Language (SOSL) in Apex

Example: Returning Records for a Particular Field Using SOSL

We want to return records that contain ‘Oil‘ text from the Name field of the account and course custom object, but only display the following fields related to a particular object.

  • Account(Name, Industry)
  • Course(Name, Course_Duration__c)
FIND { Oil } IN NAME FIELDS RETURNING Account(Name, Industry), Course__c(Name, Course_Duration__c)

The above SOSL query searches the ‘Oil’ text in only the name field. Then, it returns the matching records from an account with name and industry fields and course__c with name and course duration fields.

In the results below, the account records have been displayed, with the oil text available in the name field alongside the fields we provided.

But we also provided the course object, even though there is no record to display because none contain the oil text in the name field.

Write SOSL Query in Salesforce

Example: Use Wildcards in SOSL Query

Now, we will see how to use wildcards in SOSL queries for partial or pattern-based searches. We can use two main wildcards to retrieve the record from the Salesforce org.

Wildcard DescriptionExample
* (Asterisk)It matches zero or more characters (add middle or last to string)FIND ‘Jo*‘ (Finds “John,” “Jonathan,” “Jordan”)
? (Question Mark)It matches exactly one character (add middle or last to string)FIND ‘J?hn‘ (Finds “John” but not “Jonathan”)

Use *(Asterisk) Wildcard in SOSL

For example, we want to return records that start with Tech*‘ across all fields of the account object, and display only the account name and priority fields.

FIND {Tech*} IN ALL FIELDS RETURNING Account(Name, Account_Priority__c)

The above SOSL query searches all fields in the Salesforce org for records that start with ‘Tech’. We have used the *(Asterisk) wildcard in the SOSL query, so it will return words that contain ‘Tech’.

After that, we displayed only account records with the name and priority fields. In the image below, we also see one record that does not contain the word ‘Tech’, even though it appears in the search results.

This is because we searched all fields in the SOSL query. That means the searched text will be in any field if you open that account.

How to Use Wildcard in SOSL Query

Here, you can see that when I opened that record, the account site field shows Word, which contains the searched text.

Use Wildcard in SOSL Query

Use ?(Question Mark) Wildcard in SOSL

For example, we want to find contacts named ‘John’ but also for minor variations (e.g., “Jahn”).

FIND 'J?hn' IN NAME FIELDS RETURNING Contact(Name)

Now we know that? (Question Mark) A wildcard matches exactly one character, as shown by the symbol we added.

The above query will return the contact names, replace the wildcard character with a character, and display the records from the contact object.

In the image below, you can see the result: the text we provided with the wildcard, where one character was replaced with another.

How to Use Wildcard in SOSL Query in Salesforce

Example: Specifying Conditions and Limits in SOSL

Now, we will understand how to add conditions and limits to the RETURNING clause to filter your results. For example, we want to find the records that contain ‘oil’ text and are active from the account object.

FIND { oil } IN Name Fields RETURNING Account( Name, Active__c WHERE Active__c = 'Yes' LIMIT 5 )

In the above SOSL query in the find, we have provided ‘oil’ text from the account, and then we also have provided a WHERE clause to filter the records with the LIMIT clause, so that we also display the records with the number we provide in the limit.

In the image below, you can see the accounts that contain ‘oil’ text displayed with only active account records.

We provided the 10-record limit, but we don’t have that many records, so we displayed only four records here. If there are more than 10 records, the search results will display only 10.

How to use SOSL Query

In this way, we can specify conditions and limits in the SOSL query.

Frequently Asked Questions

Q1: When should we use SOSL?

When we don’t know which object contains the data

Q2: What is the difference between SOSL and SOQL?

SOSL = search multiple objects, SOQL = query specific object

Q3: Can SOSL be used in triggers?

No, it is mainly used in Apex classes

Q4: Does SOSL support wildcards?

Yes (* and ?)

Conclusion

Salesforce Object Search Language (SOSL) is a powerful feature that enables developers to perform fast, flexible searches across multiple objects.

It is mainly used when the exact location of data is unknown. By understanding its syntax, use cases, and best practices, you can build efficient search functionality in Salesforce applications.

When used correctly, SOSL improves performance and user experience, especially in applications that require searching large amounts of data. Combining SOSL with Apex logic enables developers to create dynamic, user-friendly solutions.

You may also like to read:

Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

Salesforce flows complete guide

FREE SALESFORCE FLOW EBOOK

Learn how to work with flows in Salesforce with 5 different real time examples.