Sub topic 13
Enforcing sharing and field-level security
This chapter assumes you can write inline SOQL in Apex, use bind variables, and know the basics of Salesforce sharing and profiles or permission sets. It covers how to make a query respect the running user's access.
Why queries ignore permissions by default
Apex usually runs in system mode. A query in system mode ignores the user's object permissions and field-level security (FLS). Sharing rules depend on the class declaration. A class declared without sharing, or one with no sharing keyword, can return records the user cannot see. A class declared with sharing enforces record-level sharing, but it still does not check object or field permissions.
That gap causes the common error. A developer marks a class with sharing and assumes the user can only see permitted data. The user can still read a field they have no access to, because with sharing does not check FLS.
| Mechanism | Record sharing | Object permissions | Field-level security |
|---|---|---|---|
| Default query in a `without sharing` class | No | No | No |
| Default query in a `with sharing` class | Yes | No | No |
| `WITH USER_MODE` | Yes | Yes | Yes |
Using WITH USER_MODE
Add WITH USER_MODE to the query. The query then runs with the current user's sharing, object permissions, and FLS.
List<Account> accts = [
SELECT Id, Name
FROM Account
WHERE Industry = 'Technology'
WITH USER_MODE
];If the user cannot read Account, or cannot read Name or Industry, the query throws a QueryException. It does not return partial results. Fields used in WHERE and ORDER BY are checked as well as the selected fields.
Handling the failure
Catch the exception when a missing permission should not stop the whole operation.
try {
List<Account> accts = [
SELECT Id, Name FROM Account WITH USER_MODE
];
System.debug(accts.size() + ' accounts visible');
} catch (QueryException e) {
System.debug('Access denied: ' + e.getMessage());
}Alternatives and related options
WITH SECURITY_ENFORCEDis the older clause. It checks object and field access but not the user's sharing, and it only checks theSELECTfields. PreferWITH USER_MODEin new code.Security.stripInaccessibleremoves fields the user cannot read instead of throwing. Use it when you want a partial result.Database.queryandDatabase.queryWithBindsaccept anAccessLevel.USER_MODEargument for dynamic queries, covered in a later chapter.
Common mistakes
- Assuming
with sharingchecks FLS. It does not. - Adding
WITH USER_MODEand testing only as an administrator. Admins usually have full access, so the failure never shows up. Test with a user who has a limited profile or permission set. - Using system mode on purpose, then returning the data to the user without checking access. Run in system mode only when the logic needs it, and keep the returned data limited.
Exercise
A class is declared with sharing. Its query selects AnnualRevenue, and the running user has no read access to that field. What does the query return, and what change makes it respect the field permission?
Show answer
It returns the field value, because with sharing enforces only record-level sharing and does not check FLS. Add WITH USER_MODE to the query. The query then throws a QueryException for the inaccessible field, which you can catch.