Sub topic 6
Choosing objects and fields with RETURNING
This chapter assumes you know basic SOQL, Apex collections, and how to run anonymous Apex. It covers the RETURNING clause of a SOSL query. FIND decides which records match. RETURNING decides which objects and fields come back.
What RETURNING does
A SOSL search looks across many objects at once. Without RETURNING, a query returns only the Id of every searchable object that has a match. In practice you almost always list the objects and fields you want.
FIND {Acme*} IN NAME FIELDS
RETURNING Account(Id, Name), Contact(Id, FirstName, LastName)Each entry in RETURNING has the form ObjectName(field1, field2, ...). Separate entries with commas.
- Every listed object gets its own result list.
- Only the fields you name are populated on the returned records. Id is always included.
- Objects you leave out are not searched, even if they contain matching text.
How results map to the list
In Apex, a SOSL query returns a List<List<SObject>>. The outer list has one inner list per object in RETURNING, in the same order you wrote them. Index 0 is the first object, index 1 is the second, and so on.
List<List<SObject>> results = [
FIND 'Acme*' IN NAME FIELDS
RETURNING Account(Id, Name), Contact(Id, FirstName, LastName)
];
List<Account> accounts = (List<Account>) results[0];
List<Contact> contacts = (List<Contact>) results[1];
for (Account a : accounts) {
System.debug('Account: ' + a.Name);
}The cast is required because the outer list holds the generic SObject type.
Choosing fields
Pick fields the same way you would in a SOQL SELECT list. Request only what your code reads. If you read a field you did not request, Apex throws a System.SObjectException at run time. This is the most common RETURNING error.
List<List<SObject>> results = [
FIND 'Acme*' IN NAME FIELDS
RETURNING Account(Id, Name)
];
Account a = (Account) results[0][0];
// a.Industry would throw SObjectException: field was not queriedSome points about fields:
- A field in RETURNING does not need to be a searched field.
IN NAME FIELDSlimits where the text is matched, not which fields you can return. - Parent fields work through relationships, such as
Contact(Id, Name, Account.Name). - Fields you cannot see are not an automatic error in plain queries. Field-level security is covered in the chapter on limits and security.
Per-object options
Each object in RETURNING can carry its own WHERE, ORDER BY, and LIMIT, written inside the parentheses after the field list. Later chapters treat these in detail. This short example shows the placement and a bind variable.
String term = 'Acme';
List<List<SObject>> results = [
FIND :term IN ALL FIELDS
RETURNING
Account(Id, Name WHERE Industry = 'Technology' ORDER BY Name LIMIT 10),
Contact(Id, Name WHERE Email != null)
];The filter on Account does not affect Contact. A Contact that matches the term is still returned if it has an email, whatever its account's industry.
Common mistakes
| Mistake | Result | Fix |
|---|---|---|
| Reading `results[1]` when RETURNING lists one object | `ListException`: index out of bounds | Match indexes to the RETURNING order |
| Reading an unrequested field | `SObjectException` | Add the field to the object's field list |
| Omitting RETURNING | Only Ids returned for searchable objects | List the objects and fields you need |
| Reordering RETURNING without updating indexes | Casting error or wrong data | Keep a comment or constants for the order |
Exercise
A query uses RETURNING Contact(Id, Name), Lead(Id, Name). Which index holds the Lead records, and what happens if your code then reads lead.Email?
Show answer
Lead records are at results[1], because Contact is listed first. Reading lead.Email throws a System.SObjectException, because Email was not included in the Lead field list.
Exercise
Write a RETURNING clause that returns Account Id and Name, limited to 5 accounts ordered by Name, plus Opportunity Id and Name.
Show answer
RETURNING Account(Id, Name ORDER BY Name LIMIT 5), Opportunity(Id, Name)