Intermediate 15 min read

SOSL Basics

By the end of this lesson, you'll be able to:

  • Write a basic SOSL FIND query
  • Scope a SOSL search to specific objects and fields with RETURNING

Prerequisites: Date Literals and Relative Date Filtering

FIND and search groups

FIND {Acme} IN ALL FIELDS RETURNING Account, Contact, Lead

This searches indexed text fields across all three objects simultaneously. IN ALL FIELDS can be narrowed to IN NAME FIELDS or IN EMAIL FIELDS for a faster, more targeted search.

RETURNING with field lists

Each object listed in RETURNING can specify its own field list, and even its own WHERE clause:

RETURNING Contact(Id, Name WHERE MailingState = 'Gauteng')

Searching Account and Contact names

FIND {Acme} IN NAME FIELDS RETURNING Account(Id, Name), Contact(Id, Name, Email)

This searches only the Name-type fields on Account and Contact for the term 'Acme', returning each object's own field list separately.

Exercise

Write a SOSL search for the term 'Smith' across Contact and Lead, returning just Id and Name from each.

Show hint

FIND {Smith} IN NAME FIELDS RETURNING Contact(Id, Name), Lead(Id, Name)

SOQL

SOSL Basics — Quick Check

1. Which keyword starts every SOSL search?

2. A single SOSL query can search and return results from multiple different objects at once.

3. What does RETURNING control in a SOSL query?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

SOSL searches a text index across multiple objects and fields at once with FIND, narrowing results per object using RETURNING — the right tool when you don't know which object contains the term you're looking for.