Sub topic 8
Running SOSL in Apex and reading the result lists
This chapter assumes you can write a basic FIND query with a RETURNING clause, and that you know Apex collections and casting. Here you will run SOSL inside Apex and read the nested result lists correctly.
Static SOSL in Apex
Put a SOSL query in square brackets to run it as static SOSL. The compiler checks the syntax and the object and field names when you save the class.
List<List<SObject>> results = [
FIND 'Acme*' IN NAME FIELDS
RETURNING Account(Id, Name), Contact(Id, FirstName, LastName)
];The return type is always List<List<SObject>>. It is a list of lists because one query can search several objects, and each object gets its own inner list.
Reading the result lists
The inner lists appear in the same order as the objects in RETURNING. In the query above, results[0] holds the accounts and results[1] holds the contacts. Each inner list is typed as List<SObject>, so you must cast it to the concrete type before you read fields.
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);
}
for (Contact c : contacts) {
System.debug('Contact: ' + c.FirstName + ' ' + c.LastName);
}If an object has no matches, its inner list is empty. The outer list still has one entry for every object in RETURNING, so indexing by position is safe.
Order matters
Reordering RETURNING changes the indexes. If you change it to RETURNING Contact(...), Account(...), then results[0] holds contacts, and casting it to List<Account> throws a System.TypeException at run time. The compiler cannot catch this, because both inner lists are List<SObject>.
Results are not in a guaranteed order
Within each inner list, records come back by relevance unless you add ORDER BY to that object's clause. Do not assume the first record is the best match unless you have sorted it yourself.
Bind variables
A static query can use Apex variables with a colon prefix. This keeps the query readable and lets the platform handle the value safely.
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)
];Bind variables work in the search term and in the per-object WHERE and LIMIT clauses. Later chapters cover them in more detail, along with dynamic queries built with Search.query.
Common mistakes
- Casting
results[0]to the wrong type after you reorder RETURNING. - Reading a field you did not list in RETURNING. Apex throws a
System.SObjectExceptionwhen you access it. - Assuming a missing object produces a missing list. The list is present and empty.
- Putting a search term shorter than two characters in the query, which raises a runtime error.
Exercise
A query is written as FIND 'Globex*' IN NAME FIELDS RETURNING Opportunity(Id, Name), Account(Id, Name). Which index holds the accounts, and what line gets them into a typed list?
Show answer
Accounts are at index 1, because Opportunity is listed first. Use List<Account> accounts = (List<Account>) results[1];