Searching Multiple Objects
By the end of this lesson, you'll be able to:
- Search and return results from more than one object in a single SOSL statement
- Limit which fields are searched using IN NAME FIELDS or IN specific field groups
- Process each object's results separately after the search runs
Prerequisites: "Your First SOSL Search"
Searching several objects at once
List<List<sObject>> searchResults = [
FIND 'Riverbend' IN ALL FIELDS
RETURNING Account(Id, Name), Contact(Id, LastName, Email), Lead(Id, Company)
];
List<Account> accounts = searchResults[0];
List<Contact> contacts = searchResults[1];
List<Lead> leads = searchResults[2];
System.debug('Found ' + accounts.size() + ' accounts, ' + contacts.size() + ' contacts, ' + leads.size() + ' leads.');
List<List<sObject>> searchResults = [— this is the direct payoff from this module's first lesson.FIND 'Riverbend' IN ALL FIELDS— one search text.RETURNING Account(Id, Name), Contact(Id, LastName, Email), Lead(Id, Company)— three object types, in this order.];— closes the search — a single round trip instead of three separate SOQL queries.- (blank line)
List<Account> accounts = searchResults[0];— the first object's matches.List<Contact> contacts = searchResults[1];— the second's.List<Lead> leads = searchResults[2];— the third's.- (blank line)
System.debug('Found ' + accounts.size() + ' accounts, ' + contacts.size() + ' contacts, ' + leads.size() + ' leads.');— reports all three counts at once.
Narrowing the search with IN NAME FIELDS
List<List<sObject>> searchResults = [
FIND 'Riverbend' IN NAME FIELDS
RETURNING Account(Id, Name)
];
List<List<sObject>> searchResults = [— opens the search.FIND 'Riverbend' IN NAME FIELDS—IN NAME FIELDSnarrows the search specifically to name-type fields (likeAccount.NameorContact.LastName) — useful when you know you're searching for a name specifically, not scanning notes or descriptions too.RETURNING Account(Id, Name)— the object and fields to return.];— closes the search.
A real business example: Global Support Search
String searchTerm = 'connectivity issue';
List<List<sObject>> searchResults = [
FIND :searchTerm IN ALL FIELDS
RETURNING Case(Id, Subject, Status), Knowledge__kav(Id, Title)
];
String searchTerm = 'connectivity issue';— the search text as an Apex variable.- (blank line)
List<List<sObject>> searchResults = [— opens the search.FIND :searchTerm IN ALL FIELDS— the:searchTermbind variable — Module 20's bind variable syntax works in SOSL too.RETURNING Case(Id, Subject, Status), Knowledge__kav(Id, Title)— both openCaserecords and a knowledge base article object searched at once.];— closes the search.
A support agent's global search box searching both at once is a textbook SOSL scenario — one search box, multiple object types, exactly the shape this module exists for.
Exercise
Write a SOSL search for 'Dlamini' across both Contact (Id, LastName) and Lead (Id, LastName), then debug each list's size.
Show hint
RETURNING Contact(Id, LastName), Lead(Id, LastName)
Searching Multiple Objects Quiz
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
A single SOSL search can span several objects at once — genuinely useful whenever a search term could plausibly belong to more than one kind of record.