Salesforce Object Search Language

  1. Complete Guide to Salesforce Object Search Language (SOSL)

Introduction

Salesforce Object Search Language (SOSL) is a powerful search language used in Salesforce Apex to perform text-based searches across multiple objects. Unlike SOQL, which queries specific objects, SOSL searches all records in Salesforce that match the given text, making it highly useful for global searches.

This guide covers:
✔ What is SOSL?
✔ SOSL vs. SOQL – Key Differences
✔ How to Write SOSL Queries in Apex
✔ Best Practices for Optimized Searches


1. What is SOSL?

SOSL allows users to search multiple Salesforce objects at once based on text, keywords, or partial matches. It’s useful when you don’t know the exact record location and need a quick global search.

Key Features of SOSL:

Multi-Object Search – Finds data in multiple objects with a single query.
Fuzzy Matching – Returns records even with partial matches.
Efficient & Fast – Uses Salesforce’s full-text search index for speed.
Limited to Fields with Search Indexing – Works best on Name, Email, and custom fields with search enabled.


2. SOSL vs. SOQL – Key Differences

FeatureSOSL (Search)SOQL (Query)
ScopeSearches across multiple objectsQueries a single object at a time
Search TypeFinds text-based recordsRetrieves structured data
Wildcard SupportUses FIND 'John*' for partial matchesNo wildcard support in WHERE clause
PerformanceFaster for large text searchesBetter for structured queries
Best Used ForFinding records without knowing exact locationsRetrieving specific records with conditions

3. Writing a Basic SOSL Query in Apex

Example: Searching for a Contact by Name

apex
List<List<SObject>> searchResults = [FIND 'John' IN ALL FIELDS RETURNING Contact(Id, Name, Email)];
for (SObject obj : searchResults[0]) {
Contact con = (Contact)obj;
System.debug('Found Contact: ' + con.Name + ' - ' + con.Email);
}

Explanation:

FIND 'John' → Searches for “John” in indexed fields.
IN ALL FIELDS → Searches across all standard and custom fields.
RETURNING Contact(Id, Name, Email) → Returns matching Contacts.
searchResults[0] → Stores results as a list of SObjects.


4. Advanced SOSL Queries

🔹 Searching Multiple Objects at Once

apex
List<List<SObject>> results = [FIND 'Acme Corp' IN NAME FIELDS RETURNING Account(Id, Name), Contact(Id, Name)];

✔ Searches for "Acme Corp" in the Name fields of Accounts and Contacts.

🔹 Using Wildcards in SOSL

apex
List<List<SObject>> results = [FIND 'Mark*' IN ALL FIELDS RETURNING Contact(Id, Name)];

✔ Finds all Contacts whose name starts with “Mark”.

🔹 Searching Specific Fields

apex
List<List<SObject>> results = [FIND 'support' IN EMAIL FIELDS RETURNING Case(Id, Subject)];

✔ Searches for the term "support" only in Email fields of the Case object.


5. Best Practices for SOSL Queries

Use Specific Search Terms – Avoid generic words like “test”.
Limit Fields in RETURNING Clause – Fetch only the required data.
Use SOSL for Large Searches, SOQL for Precise Queries.
Avoid Overloading with Multiple Objects – Stick to essential objects for better performance.
Ensure Fields are Searchable – Only indexed fields can be searched via SOSL.


6. When to Use SOSL Over SOQL?

🚀 Use SOSL when:
✔ Searching for a record without knowing its object.
✔ Looking for text matches across multiple objects.
✔ Searching long text fields like descriptions or case comments.

🛑 Use SOQL when:
✔ Querying a specific object with filters.
✔ Needing structured data with conditions (WHERE, ORDER BY).
✔ Fetching related records via relationships (INNER JOIN).


Conclusion

SOSL is a powerful tool for text-based searches across multiple Salesforce objects, making it ideal for global search functionalities. By using SOSL efficiently, you can improve data retrieval performance and build better search-driven applications in Salesforce.

🔹 Want more examples? Let me know! 😊 We are here to help you with your better future growth. We also provide consultation for guidance and roadmap to your future in Salesforce. Script and code Learn with us, Grow with us. Thank you! Happy Learning!22

salesforce object search language
Share your love

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Your email address will not be published. Required fields are marked *