Перейти к содержимому

stripInaccessible and Describe Calls

Это содержимое пока не доступно на вашем языке.

Module 4 of 7 in the chapter Sharing When Writing Apex: Secure Code, chapter 17 of Securing Salesforce: Sharing and Visibility. Each module stands on its own.

The question this module answers: User mode stops when a user can’t reach a field. Sometimes that’s right, and sometimes the screen should just show what it can. How do you choose, and how do you check for yourself?

Ten years and more ago, there was no user mode. If you wanted to respect a user’s field-level security in code, you wrote it yourself: query the schema, ask can this user delete, can they read this field, and hide what they couldn’t see. A lot of crazy code. Some of that is still the right tool. Most of it now has a better one.

When code meets a field the user can’t reach, it can do one of two things:

  • Fail. Stop and say which field. That’s user mode, from module 3: an error, with getInaccessibleFields() or getDmlFieldNames() to name the field.
  • Leave it out. Carry on without the field, the way a page layout or a report simply doesn’t show it. That’s Security.stripInaccessible.

Salesforce describes the second as “graceful degradation of the application by omitting fields, rather than failing outright,” and says it “is similar to the behavior with views, reports, and layouts” (Enforce Security with the stripInaccessible Method (opens in a new tab), Apex Developer Guide, checked 2026-11-26).

The choice is a business one. If the process can’t work without the field (a save that must set it), fail, and say why. If the field is one more thing to show (a column some people see and some don’t), leave it out. Salesforce’s own example is that second kind: “A display table for campaign data must always show the BudgetedCost. The ActualCost must be shown only to users who have permission to read that field” (same page).

stripInaccessible: leaving it out, on purpose

Section titled “stripInaccessible: leaving it out, on purpose”

What it does, in Salesforce’s words (same page):

  • It strips “fields and relationship fields from query and subquery results that the user can’t access.”
  • It can “remove inaccessible sObject fields before DML operations to avoid exceptions.”
  • It can “sanitize sObjects that have been deserialized from an untrusted source,” such as data arriving through “Apex REST, Lightning clients, and so on.”
  • The check is “based on the field-level permission of the current user in the context of the specified operation: create, read, update, or upsert.”
  • “The ID field is never stripped,” the records come back “in the same order,” and it “doesn’t support AggregateResult SObject.”

That third point is worth an architect’s attention. A record built from a request, a screen or an integration can carry any field its sender put in it. Strip it for the operation you’re about to do, and a user can’t slip in a field they’re not allowed to set.

And notice what it doesn’t do: it’s “field-level and object-level data protection.” It decides which fields come back, not which records. The records are still the sharing keyword’s job (module 2).

Here’s the display case as a sketch (not compiled), on the restaurant orders from chapter 15, where only some staff may see an internal cost:

public with sharing class OrderTable {
@AuraEnabled
public static List<Order> rows() {
// Reason for system mode: return every column, then strip per user.
// Records are still limited by "with sharing".
List<Order> orders = [SELECT Id, Status, Delivery_Note__c, Internal_Cost__c
FROM Order WITH SYSTEM_MODE];
SObjectAccessDecision decision =
Security.stripInaccessible(AccessType.READABLE, orders);
return decision.getRecords();
}
}

Two settings, two jobs: with sharing decides the records, and stripInaccessible decides the fields. And the system-mode query has its reason written beside it, as module 7 asks.

The oldest tool is still the most precise. “At the most granular level, you can enforce object-level and field-level permissions in your code by explicitly calling the Schema.DescribeSObjectResult and the Schema.DescribeFieldResult methods to check the current user’s access permission levels” (Enforce Security with Field and SObject Describe Methods (opens in a new tab), Apex Developer Guide, checked 2026-11-26).

These are the calls I used to write by hand: “you can call the isAccessible, isCreateable, or isUpdateable methods of Schema.DescribeSObjectResult to verify whether the current user has read, create, or update access to an sObject,” the same on a field, and “the isDeletable method provided by Schema.DescribeSObjectResult to check if the current user has permission to delete a specific sObject” (same page).

Where they still earn their place: deciding before you do any work. Should the page show the Delete button at all? Should a long-running job start, if the user can’t save the result? Ask first, and nobody gets halfway through something they were never allowed to finish.

Three limits from Salesforce, worth knowing before you promise anything (Enforce Object and Field Permissions (opens in a new tab), Apex Developer Guide, checked 2026-11-26):

  • Site users’ personal information. Experience Cloud’s settings to hide a user’s personal information “aren’t enforced in Apex, even with security features such as the WITH USER_MODE clause or the stripInaccessible method.” Chapter 14 showed those settings (chapter 14, module 8); code that reads user records has to leave those fields out itself.
  • The Automated Process user. “Automated Process users can’t perform Object and FLS checks in custom code unless appropriate permission sets are explicitly applied to those users.”
  • Permissions win over sharing. “Sometimes, the access level granted by a sharing rule can conflict with an object-level or field-level permission. In that case, object-level and field-level permissions take precedence over sharing rules.” A share that gives Read/Write doesn’t let a user edit a field their field-level security hides.
Three ways code handles a field the user can't reach Three ways code handles a field the user can't reach. Fail, with user mode: it stops and names the field with getInaccessibleFields() or getDmlFieldNames(); right when the process needs the field. Leave it out, with Security.stripInaccessible: it removes the fields the user can't read, create or update, from results, before saves and from untrusted input; fields only, not records; right when some people see a column. Ask first, with describe calls: isAccessible, isCreateable, isUpdateable, and isDeletable on the object; right before doing work, such as whether to show a button. Footnote: none of them enforce Experience Cloud personal information settings; leave those fields out yourself. {"author": "Steve Simpson", "blueprint": "1.0.0", "builder": "standard 1.59.0", "built": "2026-11-20", "id": "DGM-SVA-SC-04", "rights": "\u00a9 2026 Twinshark LLC. All rights reserved.", "template": "comparison 0.5.0", "theme": "stevetecharc 1.4.0"} Fail user mode Stops, and names the field getInaccessibleFields(), getDmlFieldNames() When the process needs the field Leave it out Security.stripInaccessible Removes fields the user can't read, create or update Results, before saves, untrusted input Fields only, not records When some people see a column Ask first describe calls isAccessible, isCreateable, isUpdateable isDeletable on the object Before doing work: show the button or not None of them enforce Experience Cloud personal information settings: leave those fields out yourself. © 2026 Twinshark LLC · stevetecharc.com
Click to enlarge

Test knowledge. Know the choice: user mode fails on an inaccessible field and names it; Security.stripInaccessible removes inaccessible fields and relationship fields for an AccessType (create, read, update, upsert), from query and subquery results, before DML, and from deserialized untrusted input, never stripping the ID and not supporting AggregateResult; describe calls (isAccessible, isCreateable, isUpdateable, isDeletable) check first. Know that stripInaccessible handles fields and objects, not records; that Experience Cloud personal information settings aren’t enforced even by user mode or stripInaccessible; that Automated Process users need permission sets for these checks; and that object and field permissions take precedence over sharing rules. A likely area, not a known question.

  • For each screen, integration and action: which fields the process needs (fail if missing) and which are extras (leave out).
  • That every record built from a request or an integration is stripped for its operation before it’s saved.
  • That records and fields are handled by two settings, and neither is mistaken for the other.
  • How user records are read in code on Experience Cloud sites, since the personal information settings don’t reach Apex.
  • That the Automated Process user has the permission sets its code’s checks need.

Beyond the exam: what AI changes. An agent action often returns a whole record to the agent, and the agent repeats it. Stripping that record for the user before returning it is the difference between an agent that shows the internal cost to everyone and one that shows it only to the people who can already see it. And input an agent builds for a save is untrusted input: strip it for the operation, like anything else that comes in. Chapter 20 covers agents.