Sub topic 12
Removing inaccessible fields with Security.stripInaccessible
This chapter assumes you know basic Apex and SOQL, and that you know Apex runs in system mode by default. In system mode, a query returns every field you ask for, whether or not the running user can read it. Security.stripInaccessible removes the fields the user cannot access from records you already hold.
What it does
Security.stripInaccessible(accessType, records) checks field-level security (FLS) and object permissions for the current user. It returns an SObjectAccessDecision. Its records have the inaccessible fields removed. It does not throw an exception for a hidden field.
This differs from WITH USER_MODE. A user-mode query fails with an exception when the user cannot read a queried field. stripInaccessible instead returns a cleaned copy of the data.
List<Account> raw = [SELECT Id, Name, AnnualRevenue FROM Account LIMIT 10];
SObjectAccessDecision decision = Security.stripInaccessible(AccessType.READABLE, raw);
List<Account> safe = (List<Account>) decision.getRecords();If the user cannot read AnnualRevenue, each record in safe has no value for that field. Reading it with safe[0].AnnualRevenue then throws a System.SObjectException, because the field was never populated. Only the fields that survived the check are available.
Choosing an access type
The first argument is an AccessType value. Pick the one that matches what you will do next.
| AccessType | Checks | Typical use |
|---|---|---|
| READABLE | Read access | Returning records to a client |
| CREATABLE | Create access | Before an insert |
| UPDATABLE | Edit access | Before an update |
| UPSERTABLE | Create and edit access | Before an upsert |
For input, strip the fields before the DML. The sanitized records contain only fields the user may write.
List<Account> incoming = new List<Account>{
new Account(Name = 'Acme', AnnualRevenue = 1000000)
};
SObjectAccessDecision decision = Security.stripInaccessible(AccessType.CREATABLE, incoming);
insert decision.getRecords();Finding out what was removed
The decision object reports what it removed. This is useful for logging or for telling the user why a value was ignored.
Set<String> removed = decision.getRemovedFields().get('Account');
if (removed != null && !removed.isEmpty()) {
System.debug('Removed fields: ' + removed);
}getRemovedFields() returns a map from object name to the set of removed field names. getModifiedIndexes() returns the positions of the records that lost at least one field.
Choosing between the approaches
- Use
WITH USER_MODEwhen a hidden field should stop the operation. - Use
stripInaccessiblewhen the operation should continue with only the permitted data. - Use describe checks when you need a decision about one specific field.
Exercise
A controller queries Account records in system mode and returns them to a Lightning component. A user cannot read AnnualRevenue. You want the component to still receive the accounts, without that field and without an error. What do you call, and with which AccessType?
Show answer
Call Security.stripInaccessible(AccessType.READABLE, records) and return decision.getRecords(). The accounts come back without AnnualRevenue, and no exception is thrown.