Sub topic 2
SOSL or SOQL: choosing the right tool
This chapter assumes you can write basic SOQL (SELECT, WHERE, ORDER BY, LIMIT), read Apex collections, and run anonymous Apex. It compares the two query languages so you can pick one for a given requirement.
Two languages, two jobs
SOQL (Salesforce Object Query Language) reads records from one object, plus related objects reached through relationships. You name the fields and write conditions on them. The database applies those conditions to field values.
SOSL (Salesforce Object Search Language) searches text. It looks for words in a text index and returns matching records from several objects in one call. You give it a search term, and Salesforce finds records that contain it.
| Question | SOQL | SOSL |
|---|---|---|
| Objects per query | One, plus related objects | Many |
| Matching | Field conditions such as `=`, `>`, `LIKE` | Words and phrases in a text index |
| Field types searched | Any field type | Text, email, and phone fields |
| Aggregates (`COUNT`, `SUM`, `GROUP BY`) | Yes | No |
| Apex return type | `List<SObject>` | `List<List<SObject>>` |
| Synchronous Apex query limit | 100 queries, 50,000 rows total | 20 queries, 2,000 rows per query |
Same task, both tools
Suppose a user types "Acme" into a search box. With SOQL you must pick the object and field in advance.
List<Account> byName = [
SELECT Id, Name
FROM Account
WHERE Name LIKE '%Acme%'
];This finds only accounts, and only in the Name field. To also check contacts, you need a second query, and the leading wildcard in LIKE '%Acme%' can make it slow on large tables.
The SOSL version covers both objects in one statement.
List<List<SObject>> results = [
FIND 'Acme' IN ALL FIELDS
RETURNING Account(Id, Name), Contact(Id, Name)
];The later chapters explain each clause. For now, note that one query returns one result list per object.
A decision guide
Choose SOQL when you:
- need
COUNT,SUM, orGROUP BY - filter on numbers, dates, picklists, or lookups
- need every matching row, such as in a batch job
- need records created a moment ago
Choose SOSL when you:
- build a global search box
- don't know which object or field holds the term
- want word-based matching, so a search for
Acmealso finds "Acme Corp"
Gotchas
SOSL returns at most 2,000 records per query, so it cannot page through a whole table. It also depends on a search index, and the index is updated shortly after a record is saved. A SOSL query run immediately after an insert may not find the new record. SOSL tests have to account for this, as a later chapter shows.
List<List<SObject>> found = [FIND 'Acme' IN NAME FIELDS RETURNING Account(Id)];
Map<Id, Account> hits = new Map<Id, Account>((List<Account>) found[0]);
List<Contact> contacts = [
SELECT Id, Name, AccountId
FROM Contact
WHERE AccountId IN :hits.keySet()
];Exercise
A report must show the number of opportunities per stage for this quarter. A separate search box must find a customer by any word in its name, across accounts and contacts. Which language fits each requirement, and why?
Show answer
The report needs SOQL, because it uses an aggregate (COUNT with GROUP BY StageName) and a date condition, and SOSL supports neither. The search box needs SOSL, because it matches a free-text term across two objects in one query, and SOQL would need a separate query per object.