Sub topic 11
Governor limits, sharing, and field access
This chapter assumes you can write a FIND query with a RETURNING clause and run it in Apex. It covers three things that decide whether SOSL code works in production: the limits Salesforce enforces, which records a search can return, and which fields a user can see in the results.
Governor limits for SOSL
Apex enforces limits per transaction. These are the ones that apply to SOSL:
| Limit | Value |
|---|---|
| SOSL queries issued in one transaction | 20 |
| Records returned by one SOSL query | 2,000 |
| Total heap size (synchronous) | 6 MB |
The 2,000-record cap applies to the whole query. Each object in RETURNING counts toward that total. A broad search over many objects can therefore return fewer records per object than you expect.
Every [FIND ...] expression and every Search.query call counts as one SOSL query. A search inside a loop reaches the limit of 20 quickly.
// Wrong: one SOSL query per term, so the 21st iteration throws
// System.LimitException: Too many SOSL queries: 21
for (String term : terms) {
List<List<SObject>> r = [FIND :term IN NAME FIELDS RETURNING Account(Id)];
}Run one search for all the terms instead. Join them with OR in the search expression.
public with sharing class MultiTermSearch {
public static List<Account> findAny(List<String> terms) {
List<String> safeTerms = new List<String>();
for (String t : terms) {
safeTerms.add(String.escapeSingleQuotes(t));
}
String expression = String.join(safeTerms, ' OR ');
String query = 'FIND \'' + expression + '\' IN NAME FIELDS '
+ 'RETURNING Account(Id, Name LIMIT 200)';
List<List<SObject>> results = Search.query(query);
return (List<Account>) results[0];
}
}You can check how much of the budget you have used with Limits.getSoslQueries() and Limits.getLimitSoslQueries().
Sharing: which records a search returns
SOSL respects the sharing settings of the running context, and the class declaration controls that context:
with sharingreturns only records the running user can see.without sharingreturns records regardless of the user's access.inherited sharinguses the sharing mode of the calling class.- A class with no keyword runs in the mode of its caller. If it is the entry point, it runs without sharing.
Declare the mode on every class that runs SOSL. The AccountSearch class earlier in this tutorial uses with sharing for this reason.
Field and object access
Sharing decides which records a user sees. It does not check object permissions or field-level security. By default, Apex runs in system mode for those checks, so a search can return fields the user is not allowed to read.
Add WITH USER_MODE to the query to enforce the user's object permissions and field-level security. Add it as a clause after RETURNING:
List<List<SObject>> results = [
FIND 'Acme*' IN NAME FIELDS
RETURNING Account(Id, Name, AnnualRevenue)
WITH USER_MODE
];With WITH USER_MODE, a query that selects a field the user cannot read throws a System.QueryException. Sharing rules are also enforced in this mode. The alternative is WITH SECURITY_ENFORCED, which checks field access but leaves sharing to the class declaration. Prefer WITH USER_MODE for new code.
You can also strip inaccessible fields after the query with Security.stripInaccessible(AccessType.READABLE, records). Use that when you want a partial result instead of an exception.
Common errors
- Too many SOSL queries: 21. A search sits inside a loop. Combine terms into one query.
- Fewer results than expected. The 2,000-record cap is shared across all objects in RETURNING, and sharing hides records the user cannot see. Test with a user who has the same access as your real users, not as an administrator.
- Missing field values in the UI. You may have used
stripInaccessible, which removes fields the user cannot read.
Exercise
A with sharing class runs [FIND 'Acme*' IN NAME FIELDS RETURNING Account(Id, Name, AnnualRevenue)] for a user who cannot read the AnnualRevenue field. Does the query return AnnualRevenue values, and how do you make it enforce field access?
Show answer
Yes, it returns them. with sharing only limits which records are visible, and Apex runs in system mode for field-level security by default. Add WITH USER_MODE after the RETURNING clause so the query enforces the user's object and field permissions. The query then throws a QueryException for the unreadable field. Use Security.stripInaccessible instead if you want results without that field.