RP.
Database Portals / SOSL Details

Standard Definition

Salesforce Object Search Language

The FIND Clause

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

The Search Index

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

Optimization Guardrail

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.

Search & Discovery

Why do we need 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.

Cross-Org Search

Use SOSL when you need to search for something across the entire org, scanning multiple objects simultaneously.

Global Reach

Field Specficity (PET)

SOSL is optimized to search Phone, Email, and Text type fields only (PET) for maximum efficiency.

Indexed Fields

Dynamic Retrieval

When you do not know the object that has the result set, use SOSL to find the correct data source.

Discovery Logic

Architecture

Search Structure

FIND
<searchQuery>
Search Query
IN
<searchgroup>
Field Type
RETURNING
<fieldspec>
Field Specification

Practical Application

SOSL Use Cases

Explore how to harness global search to find data across your entire Salesforce organization.

Scenario 01

The Global Lookup

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)

Scenario 02

Fuzzy Text Matching

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)

Scenario 03

The Universal Keyword Search

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)

Scenario 04

Exact Phrase Discovery

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)

Scenario 05

Contact Detail Retrieval

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)

Scenario 06

Filtered Multi-Keyword Search

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)