Sub topic 4
Querying with WITH USER_MODE
This chapter assumes you know basic Apex classes, SOQL, and how profiles and permission sets grant object permissions (CRUD) and field-level security (FLS). You should also know that Apex runs in system mode by default, and that with sharing controls only record visibility.
What the clause does
WITH USER_MODE is a clause you add to a static SOQL query. It makes the query run with the permissions of the running user instead of system mode. Salesforce checks three things:
- Object permission: the user needs read access to the queried object.
- Field-level security: the user needs read access to every field the query references.
- Sharing: the query returns only records the user can see.
The field check covers more than the SELECT list. Fields used in WHERE, ORDER BY, and relationship paths are checked too. A user who cannot read Industry cannot filter on it either, even if the field never appears in the results.
A basic example
public with sharing class AccountService {
public static List<Account> getAccounts() {
return [
SELECT Id, Name, Industry
FROM Account
WHERE Industry != null
WITH USER_MODE
LIMIT 50
];
}
}If the running user can read Account, Name, and Industry, the query returns up to 50 visible records. If the user cannot read any one of those, the query throws a QueryException. It does not return partial data.
WITH USER_MODE also enforces sharing on its own. Even so, keep with sharing on the class. The class keyword covers the rest of the code in the class. The query clause covers only that one query.
Where the clause goes
The clause comes after WHERE and before GROUP BY, ORDER BY, and LIMIT. This is the order the SOQL grammar expects:
List<Contact> contacts = [
SELECT Id, LastName, Email
FROM Contact
WHERE Email != null
WITH USER_MODE
ORDER BY LastName
LIMIT 100
];Placing it elsewhere gives a compile error.
The flow of a user mode query
How it differs from sharing alone
Without the clause, a with sharing class still reads fields the user cannot see, because sharing does not check FLS. The next table shows the difference for a user who has Account read access but no FLS on Industry.
| Query | Result for that user |
|---|---|
| `[SELECT Id, Industry FROM Account]` in a `with sharing` class | Returns rows with `Industry` populated. |
| `[SELECT Id, Industry FROM Account WITH USER_MODE]` | Throws `QueryException`. |
| `[SELECT Id, Name FROM Account WITH USER_MODE]` | Returns the visible rows. |
Common errors
- Assuming
with sharingis enough. It filters rows only. Fields the user cannot read still come back unless you addWITH USER_MODE. - Forgetting the
WHEREfields. A query can fail on a field that is not inSELECT. Check the filter and sort fields when you debug an access error. - Querying fields the user may lack. If a field is optional for your feature, do not include it in a user mode query. Otherwise one missing permission fails the whole query.
- Catching nothing. The exception propagates to the caller. The next chapter shows how to handle it.
Practice
Exercise
A user has read access to Account and to the Name field, but no read access to Industry. A with sharing class runs [SELECT Id, Name FROM Account WHERE Industry = 'Energy' WITH USER_MODE]. What happens, and why?
Show answer
The query throws a QueryException. User mode checks field-level security for fields in the WHERE clause as well as the SELECT list, so filtering on Industry fails even though Industry is not returned.
Exercise
Where in a SOQL query does WITH USER_MODE go, and why is with sharing on the class still worth keeping?
Show answer
It goes after WHERE and before GROUP BY, ORDER BY, and LIMIT. The with sharing keyword still applies sharing to the rest of the class, while WITH USER_MODE covers only the single query it is attached to.