Sub topic 3
Writing a FIND clause
This chapter assumes you can read basic SOQL, write Apex variables and loops, and run anonymous Apex. You will learn what a FIND clause contains, how its search term is quoted in different places, and how to write a first working query.
What FIND does
Every SOSL query starts with a FIND clause. It holds the text you are looking for. SOSL checks that text against a search index, not against the live tables. A SOQL WHERE clause compares field values directly. The index is built from text fields, so a record created a moment ago may not be searchable yet.
The general shape is:
FIND 'search term' [IN search group] [RETURNING object(fields)]Only FIND is required. Leaving out IN searches all fields. Leaving out RETURNING returns only the record IDs of matches in every object you can search. In practice you almost always add RETURNING, which a later chapter covers.
Quoting the search term
The search term is written differently depending on where the query runs.
| Where the query runs | How the term is written | Example |
|---|---|---|
| Static SOSL in Apex | Single-quoted string, or a bind variable | `FIND 'Acme'` or `FIND :term` |
| REST API, Workbench | Curly braces | `FIND {Acme}` |
| `Search.query` in Apex | Single-quoted string inside a text string | `'FIND \'Acme\''` |
The search text is the same in all three forms. Only the wrapper changes. Search is not case-sensitive, so acme and ACME match the same records.
A first query
This query looks for terms that start with Acme in the name fields of accounts and contacts.
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);
}A SOSL query returns a list of lists. Each inner list holds the records for one object, in the same order as the objects in RETURNING. Here results[0] holds accounts and results[1] holds contacts. You cast each inner list to its concrete type.
The * after Acme is a wildcard. It matches any number of characters, so this query finds Acme, Acme Corp, and Acmetech. A single ? matches exactly one character. A wildcard must come after at least one character, so *me is not valid.
Phrases and special characters
To search for several words together, put them in double quotes. Without the quotes, SOSL treats each word as a separate term. Here is the same idea in the braces form:
FIND {"Acme Corp"}
IN NAME FIELDS
RETURNING Account(Id, Name)Some characters have a special meaning inside a search term, including ?, *, ", ', \, &, |, !, (, ), {, }, [, ], ^, ~, :, +, and -. To search for one as plain text, put a backslash before it. In Apex string literals the backslash itself must be doubled. If the term comes from user input, use a bind variable or escape it, as a later chapter on dynamic SOSL explains.
Common errors
- Searching for a record you just inserted. The index updates after a short delay, so the query may return nothing. In tests, use fixed search results.
- Reading the wrong inner list. The list order follows
RETURNING, not alphabetical order or the order of the objects in your org. - Using a single character or a leading wildcard. SOSL rejects
FIND '*'andFIND '*corp'. Start with at least a short literal prefix. - Casting to the wrong type.
results[0]is aList<SObject>. Cast it toList<Account>only ifAccountis first inRETURNING.
Exercise
A query reads FIND 'Globex*' RETURNING Contact(Id, LastName), Account(Id, Name). Which index of the result holds the accounts, and which search group does the query use?
Show answer
The accounts are in results[1], because Contact comes first in RETURNING. No IN clause is given, so the query uses the default search group, ALL FIELDS.