Sub topic 12
Removing inaccessible fields with Security.stripInaccessible
This chapter assumes you know that Apex runs in system mode by default and ignores field-level security (FLS). It also assumes you can read basic SOQL and Apex. Security.stripInaccessible is one of the ways to close that gap. It takes records you already hold in memory and removes every field the running user is not allowed to see or write.
What the method does
The method has this signature:
SObjectAccessDecision decision = Security.stripInaccessible(
AccessType accessType,
List<SObject> sourceRecords
);An optional third argument, a Boolean named enforceRootObjectCRUD, defaults to true.
The method works in three steps:
- It inspects the fields on each record, including fields on related parent and child records returned by a query.
- It compares them with the current user's field access for the access type you chose.
- It returns an
SObjectAccessDecision. That object holds a copy of the records without the forbidden fields.
The method never throws because of a single hidden field. This is the main difference from WITH USER_MODE, which fails the whole query.
The access types are:
| AccessType | Removes fields the user cannot... | Typical use |
|---|---|---|
| `READABLE` | read | Before returning query results to a client |
| `CREATABLE` | create | Before inserting records built from user input |
| `UPDATABLE` | edit | Before updating records built from user input |
| `UPSERTABLE` | create or update, as needed for upsert | Before an upsert |
Stripping query results
This example uses READABLE to clean up a query result:
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();The query still runs in system mode, so raw contains every field you asked for. If the user cannot read AnnualRevenue, the records in safe have no value for it. Always return or use safe, not raw.
A typical use is in an @AuraEnabled method:
public with sharing class AccountController {
@AuraEnabled(cacheable=true)
public static List<Account> getTechAccounts() {
List<Account> raw = [
SELECT Id, Name, AnnualRevenue
FROM Account
WHERE Industry = 'Technology'
LIMIT 50
];
SObjectAccessDecision decision =
Security.stripInaccessible(AccessType.READABLE, raw);
return (List<Account>) decision.getRecords();
}
}The with sharing keyword controls record visibility only. It does not enforce FLS. The stripping step is what enforces field access here.
Stripping before DML
Records built from client input can contain fields the user may not edit. Strip them with UPDATABLE or CREATABLE before the DML statement:
public with sharing class AccountSaver {
public static void saveChanges(List<Account> incoming) {
SObjectAccessDecision decision =
Security.stripInaccessible(AccessType.UPDATABLE, incoming);
update decision.getRecords();
}
}If the client sent a new AnnualRevenue and the user has Read but not Edit on that field, the value is dropped and the update proceeds without it. The save succeeds, and the hidden change is not applied. Whether that is acceptable depends on your design. Some applications should reject the request instead, which the next section covers.
Finding out what was removed
The decision object also reports what changed:
getRecords()returns the cleaned records.getRemovedFields()returns aMap<String, Set<String>>. The keys are object API names and the values are the removed field names.getModifiedIndexes()returns aSet<Integer>with the positions of records that lost at least one field.
List<Account> incoming = new List<Account>{
new Account(Name = 'Acme', AnnualRevenue = 5000000)
};
SObjectAccessDecision decision =
Security.stripInaccessible(AccessType.CREATABLE, incoming);
Map<String, Set<String>> removed = decision.getRemovedFields();
if (removed.containsKey('Account')) {
throw new SecurityException(
'You cannot set these fields: ' + removed.get('Account')
);
}
insert decision.getRecords();Here the code turns silent stripping into an explicit refusal. This pattern suits APIs where a partial save would surprise the caller.
How the method decides what to do
The object-level check matters. By default, stripInaccessible also verifies the user's object permission for the access type. For example, READABLE needs Read on the object. If the user lacks it, the method throws a NoAccessException. Passing false as the third argument skips this root-object check, and only field access is evaluated:
Security.stripInaccessible(AccessType.READABLE, raw, false);Use false only when you have already decided object access some other way.
Gotchas
Reading a removed field throws
A stripped field is not set to null. It is absent from the record, as if you had never queried it. Reading it throws an SObjectException. Check before reading:
Account a = safe[0];
Decimal revenue = a.getPopulatedFieldsAsMap().containsKey('AnnualRevenue')
? a.AnnualRevenue
: null;The query itself is not restricted
Because the SOQL runs in system mode, a hidden field can still appear in a WHERE or ORDER BY clause. A user could infer a hidden value from which records come back. Stripping the output does not prevent that. If a field is sensitive enough that this matters, use WITH USER_MODE, which also checks fields in the filter.
Sharing is separate
stripInaccessible handles field and object access only. It does not remove records the user cannot see. Pair it with with sharing or user-mode queries for record access.
Strip at the boundary
Do any logic that needs hidden fields before stripping, and strip right before returning data or writing it. If you strip early and later code reads a removed field, you get the exception described above.
Choosing between the approaches
| Approach | When a field is inaccessible | Best for |
|---|---|---|
| `WITH USER_MODE` | Throws an exception | Queries where partial data would be wrong |
| `Security.stripInaccessible` | Silently removes the field | Returning data that may be partly hidden |
| Describe checks | You decide per field | Fine control over a few fields |
Exercise
A user has Read access to Account but no Read access to the custom field InternalNotesc. Your @AuraEnabled method queries `SELECT Id, Name, InternalNotes_c FROM Account in system mode and returns Security.stripInaccessible(AccessType.READABLE, raw).getRecords(). What does the client receive, and what happens if later code in the same method reads InternalNotes__c` from the stripped records?
Show answer
The client receives Account records with Id and Name but no InternalNotesc value. The query does not fail. Reading `InternalNotes__c from a stripped record throws an SObjectException, because the field was removed rather than set to null. Use getPopulatedFieldsAsMap()` to test for the field first, or read the value from the original list before stripping if the logic needs it and the value is never returned.