Sub topic 10
Older approaches: SECURITY_ENFORCED, stripInaccessible, describe checks
This chapter assumes you know Apex classes, SOQL and DML, profiles and permission sets, and the difference between sharing rules and object and field permissions. You have seen user mode already. Here you will look at the three older ways to enforce permissions, so you can read existing code and know what each one does differently.
Why these approaches still matter
User mode (WITH USER_MODE, as user, AccessLevel.USER_MODE) is the current recommendation. Plenty of production code was written before it existed, and some of that code is still the right tool for a specific job. You need to recognize all three patterns.
| Approach | Where it applies | On a violation |
|---|---|---|
| `WITH SECURITY_ENFORCED` | Static SOQL only | Throws an exception |
| `Security.stripInaccessible` | Query results or records you are about to write | Removes inaccessible fields silently |
| Schema describe checks | Any object or field you test by hand | Returns a boolean you must act on |
WITH SECURITY_ENFORCED
WITH SECURITY_ENFORCED is a SOQL clause that checks object and field permissions for the fields in the query.
public with sharing class LegacyAccountReader {
public static List<Account> getAccounts() {
return [
SELECT Id, Name, Industry
FROM Account
WITH SECURITY_ENFORCED
LIMIT 50
];
}
}If the user cannot read Account, Name, or Industry, the query throws a System.QueryException. It has three limits that user mode removes:
- It checks only the fields in the
SELECTlist and in parent relationships. Fields used only inWHEREorORDER BYare not checked, so a user can filter on a field they cannot read. - It works only on static queries. It does not cover DML.
- It reports only the first violation it finds, so the error tells you little about everything the user is missing.
Security.stripInaccessible
Security.stripInaccessible takes an AccessType and a list of records. It returns a copy of the records with the fields the user cannot access removed. It does not throw for inaccessible fields.
public with sharing class StripExample {
public static List<Account> readableOnly(List<Account> records) {
SObjectAccessDecision decision = Security.stripInaccessible(AccessType.READABLE, records);
return (List<Account>) decision.getRecords();
}
}The same method works before DML. Use AccessType.CREATABLE or AccessType.UPDATABLE to drop fields the user cannot write, then save the cleaned records.
public with sharing class StripWriteExample {
public static void saveContacts(List<Contact> contacts) {
SObjectAccessDecision decision = Security.stripInaccessible(AccessType.CREATABLE, contacts);
insert decision.getRecords();
}
}The decision object also tells you what was removed. getRemovedFields() returns a map of object names to the removed field names, and getModifiedIndexes() returns the positions of changed records. Use these if the caller needs to know the data is incomplete.
Be careful with this method. It hides the problem. A caller may think it received complete data, and a write may succeed with some fields quietly dropped.
Schema describe checks
Describe checks ask the schema whether the current user has a permission. You test each object and field yourself.
public with sharing class DescribeCheck {
public static List<Account> getAccounts() {
if (!Schema.sObjectType.Account.isAccessible()
|| !Schema.sObjectType.Account.fields.Industry.isAccessible()) {
throw new AuraHandledException('You do not have access to this data.');
}
return [SELECT Id, Name, Industry FROM Account LIMIT 50];
}
}The related methods are isAccessible(), isCreateable(), isUpdateable(), and isDeletable(). Describe checks are the most verbose option and the easiest to get wrong. Every new field in a query needs a matching check, and a forgotten check fails silently. Nothing links the check to the query.
flowchart TDComparing behavior
When to still use them
Prefer user mode for new code. It covers queries, DML, and WHERE clauses, and it also enforces sharing.
Reach for the older tools in these cases:
- You want partial data on purpose.
stripInaccessiblereturns what the user may see, where user mode would throw. - You need to build a UI hint, such as hiding a button. A describe check answers "can this user do this?" without running a query.
- You maintain code that already works and has tests. Migrate it when you next change it, not all at once.
Exercise
A query uses WITH SECURITY_ENFORCED and selects Id and Name from Account. The user can read both fields but cannot read Account.Industry. The WHERE clause is WHERE Industry = 'Energy'. Does the query throw, and which approach would catch the problem?
Show answer
It does not throw, because WITH SECURITYENFORCED checks only the fields in the SELECT list, not the WHERE clause. The user could learn which accounts are in the Energy industry without having read access to Industry. WITH USERMODE checks fields in the WHERE clause too, so it would throw a QueryException.
Exercise
You must return whatever Contact fields the current user may read, even when some are hidden, and you want to tell the caller which fields were removed. Which approach fits, and which method reports the removed fields?
Show answer
Use Security.stripInaccessible with AccessType.READABLE. The SObjectAccessDecision it returns has getRemovedFields(), which maps each object name to the set of field names that were removed.