Intermediate 20 min read

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.');
  1. List<List<sObject>> searchResults = [ — this is the direct payoff from this module's first lesson.
  2. FIND 'Riverbend' IN ALL FIELDS — one search text.
  3. RETURNING Account(Id, Name), Contact(Id, LastName, Email), Lead(Id, Company) — three object types, in this order.
  4. ]; — closes the search — a single round trip instead of three separate SOQL queries.
  5. (blank line)
  6. List<Account> accounts = searchResults[0]; — the first object's matches.
  7. List<Contact> contacts = searchResults[1]; — the second's.
  8. List<Lead> leads = searchResults[2]; — the third's.
  9. (blank line)
  10. 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)
];
  1. List<List<sObject>> searchResults = [ — opens the search.
  2. FIND 'Riverbend' IN NAME FIELDS — IN NAME FIELDS narrows the search specifically to name-type fields (like Account.Name or Contact.LastName) — useful when you know you're searching for a name specifically, not scanning notes or descriptions too.
  3. RETURNING Account(Id, Name) — the object and fields to return.
  4. ]; — 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)
];
  1. String searchTerm = 'connectivity issue'; — the search text as an Apex variable.
  2. (blank line)
  3. List<List<sObject>> searchResults = [ — opens the search.
  4. FIND :searchTerm IN ALL FIELDS — the :searchTerm bind variable — Module 20's bind variable syntax works in SOSL too.
  5. RETURNING Case(Id, Subject, Status), Knowledge__kav(Id, Title) — both open Case records and a knowledge base article object searched at once.
  6. ]; — 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)

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

Searching Multiple Objects Quiz

1. What does IN NAME FIELDS do compared to IN ALL FIELDS?

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

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.