Sub topic 11
Choosing the right enforcement approach
This chapter assumes you know the earlier material: Apex runs in system mode by default, with sharing controls record access only, and user mode applies object permissions (CRUD), field-level security (FLS), and sharing. Here you will pick among the four enforcement options for a given piece of code.
The four options
Apex gives you these ways to enforce permissions:
- User mode (
WITH USER_MODE,as user,AccessLevel.USER_MODE) WITH SECURITY_ENFORCEDSecurity.stripInaccessible- Schema describe checks such as
isAccessible()
They differ in what they check, what happens on failure, and how much code you write.
| Approach | Checks CRUD and FLS | Checks sharing | Fields in WHERE | On failure |
|---|---|---|---|---|
| User mode | Yes | Yes | Yes | Throws an exception, or returns per-record errors with `allOrNone` false |
| `WITH SECURITY_ENFORCED` | Yes, for SELECT fields and objects | No | No | Throws `QueryException` |
| `Security.stripInaccessible` | Yes, for fields in the records | No | Not applicable | Removes fields silently |
| Describe checks | Only what you code | No | Only what you code | Whatever you write |
User mode is the only option that checks the fields in WHERE and ORDER BY clauses. It also applies sharing rules of the running user, even if the class is declared without sharing.
Default to user mode
For new code, start with user mode. It is one keyword on the statement, it covers queries and DML, and it fails closed. A user who lacks access gets an error, not partial data.
public with sharing class OrderReader {
public static List<Account> readStrict() {
return [
SELECT Id, Name, AnnualRevenue
FROM Account
WHERE AnnualRevenue > 1000000
WITH USER_MODE
LIMIT 50
];
}
}If the user cannot read AnnualRevenue, the query throws a QueryException. That includes the case where the field appears only in the WHERE clause.
When to choose something else
Partial data is acceptable
Sometimes you want to show whatever the user can see, such as a record page that hides unreadable fields. User mode would fail the whole query. In that case, query normally in a with sharing class and strip the fields afterward.
public with sharing class OrderReader {
public static List<Account> readLenient() {
List<Account> rows = [
SELECT Id, Name, AnnualRevenue
FROM Account
LIMIT 50
];
SObjectAccessDecision decision =
Security.stripInaccessible(AccessType.READABLE, rows);
return (List<Account>) decision.getRecords();
}
}stripInaccessible also works before DML. Use AccessType.CREATABLE or AccessType.UPDATABLE to remove fields the user cannot write, then save the cleaned records.
Maintaining older code
WITH SECURITY_ENFORCED still works, so you do not need to rewrite every query at once. When you touch a query that uses it, switch to WITH USER_MODE. This closes the WHERE clause gap and adds sharing enforcement. You cannot combine both clauses in one query.
Custom logic or messages
Describe checks suit cases where you need to decide something other than "allow or throw". For example, you might hide a button when the user cannot edit a field.
Boolean canEditRevenue =
Schema.sObjectType.Account.fields.AnnualRevenue.isUpdateable();Do not use describe checks as your main enforcement for queries and DML. They are easy to forget on one field, and they check only what you wrote.
A decision path
Common mistakes
- Treating
with sharingas a permission check. It controls record visibility only. - Using
WITH SECURITY_ENFORCEDand assumingWHEREfields are checked. They are not. - Wrapping
stripInaccessiblearound a query and forgetting that sharing still depends on the class keyword. - Mixing approaches on the same operation. Pick one per query so the behavior is predictable.
Exercise
A Lightning component lists accounts for any user. Some users cannot read the AnnualRevenue field, and the product owner wants those users to still see the rest of each account. Which approach fits, and why would WITH USER_MODE not?
Show answer
Use a query in a with sharing class followed by Security.stripInaccessible(AccessType.READABLE, rows). It removes AnnualRevenue for users who cannot read it and keeps the remaining fields. WITH USER_MODE would throw a QueryException for those users, so they would see no accounts at all.