User Mode, and Why WITH SECURITY_ENFORCED Went Away
هذا المحتوى غير متوفر بلغتك بعد.
Module 3 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 checks a user’s records, objects and fields in one go. How do you ask for it, what does it check that the old clause didn’t, and when is system mode still right?
In my 2023 class I was honest with the room. There were two settings, WITH SECURITY_ENFORCED and user mode, and I wanted to dig in and see the difference, because I didn’t know if I could truly articulate it. So I did what a lot of us do now: I asked an AI. And I told the class what I’ll tell you: never trust an AI to be completely correct, but it can give you some ideas. Don’t bet your life on it, but it tells you where to hunt. What I came away with was that they both sound like they do similar things, with slight spins. That wasn’t an answer.
Here’s the answer, from Salesforce’s own pages, which is where the hunt should always end.
What user mode checks
Section titled “What user mode checks”“Apex database operations run in user mode by default, which means that they apply the sharing rules, field-level security (FLS), and object permissions of the running user.” They ignore FLS and object permissions only when you set system mode explicitly (Set an Access Mode for Database Operations (opens in a new tab), Apex Developer Guide, checked 2026-11-26).
All three of chapter 4’s and Part 2’s locks, in one setting: the records the user’s sharing gives them, the objects their permissions open, and the fields their field-level security shows. And “If you set a database operation to user mode, the operation always respects the user’s sharing rules,” whatever the class’s keyword says (same page; module 1).
How you ask for it
Section titled “How you ask for it”Three forms, one for each kind of operation (same page; chapter 5 module 6 shows the syntax):
- A query: “insert a WITH USER_MODE or WITH SYSTEM_MODE clause.”
- A save: “insert the as user or as system keywords between the DML operator and the object name.”
- A Database or Search method: “An accessLevel parameter in Database and Search methods specifies whether the method runs in user mode (AccessLevel.USER_MODE) or system mode (AccessLevel.SYSTEM_MODE).” That covers dynamic queries, query locators, counts, cursors,
Search.queryand the Database insert, update, upsert, merge, delete, undelete and convertLead methods.
And some newer methods won’t let you leave it out: the ...WithBinds methods, such as Database.queryWithBinds, “require the accessLevel parameter” (same page).
Here’s the habit as a sketch (not compiled), on the restaurant orders from chapter 15:
public with sharing class OrderNotes { public static void addNote(Id orderId, String note) { // Read and save as the user: their records, objects and fields. Order o = [SELECT Id, Delivery_Note__c FROM Order WHERE Id = :orderId WITH USER_MODE]; o.Delivery_Note__c = note; update as user o; }}At API 67.0 and later, user mode is the default anyway. Write it anyway. The next person to read the class shouldn’t have to know its API version to know what it does.
Why WITH SECURITY_ENFORCED went away
Section titled “Why WITH SECURITY_ENFORCED went away”The old clause is gone: “Apex classes set to API version 67.0 and later that include WITH SECURITY_ENFORCED don’t compile” (The WITH SECURITY_ENFORCED SOQL Clause is Removed (opens in a new tab), Salesforce Summer ‘26 Release Notes, checked 2026-11-26).
And Salesforce gives the reasons, four of them, which finally settle my 2023 question. WITH USER_MODE replaces it “because of these additional advantages” (same page):
- “WITH USER_MODE accounts for polymorphic fields, such as Owner and Task.whatId.”
- “WITH USER_MODE processes all clauses in the SOQL SELECT statement including the WHERE clause.”
- “WITH USER_MODE finds all FLS errors in your SOQL query, while WITH SECURITY_ENFORCED finds only the first error.”
- “In user mode, you can use the getInaccessibleFields() method on QueryException to examine the full set of access errors.”
The second one matters most to an architect, and it was an interesting choice for Salesforce to make. Should a query be able to filter on a field the user can’t see? There are real technical scenarios where you’d want that: act on the field, but never show it. It’s like raising the query to system context just for the filter. The old clause allowed it, because it checked only the fields a query returned, not the ones in its WHERE clause.
But it helps one way and hurts the other. With deep enough knowledge of the data, someone can watch which records come back and work out what the hidden field holds. Ask for the orders where a hidden “discount approved” field is true, and the list you get back answers the question the field was hiding.
User mode takes the safe side: it checks the whole query, WHERE clause included. If you have one of those scenarios where code really must filter on a field the user can’t see, that no longer happens by accident. It’s a query you run in system mode, on purpose, with the reason written down (System mode: the exception you write down).
So: not similar things with slight spins. User mode checks more, and tells you more.
When it says no, it tells you which field
Section titled “When it says no, it tells you which field”Because user mode checks everything, it also reports everything, which means code can fail helpfully instead of mysteriously (Set an Access Mode for Database Operations):
- On a query,
getInaccessibleFields()on the QueryException gives “the full set of access errors.” - On a save, “you can use the DMLException method getDmlFieldNames() to obtain the fields with FLS errors.”
- On a Database method in user mode, “you can access errors via SaveResult.getErrors().getFields().”
That’s the difference between “Insufficient privileges” and “you don’t have access to the Delivery Note field.” Your support team will thank you. Module 4 is about the other choice: leaving the field out quietly instead of failing.
System mode: the exception you write down
Section titled “System mode: the exception you write down”System mode still exists, and sometimes it’s right: the narrow elevation in module 2, a job that has to total every order, an integration that maintains data no single user owns. Ask for it the same way, WITH SYSTEM_MODE, as system, AccessLevel.SYSTEM_MODE, and remember what it does: no object or field checks, and records decided by the class’s keyword (module 1).
Salesforce’s own words for why the default flipped: “Database operations bypass object- and field-level security settings only if you explicitly set them to run in system mode. This requirement promotes intentionality and ensures that you use system mode only when necessary” (Database Operations Run in User Mode by Default, Not System Mode (opens in a new tab), Salesforce Summer ‘26 Release Notes, checked 2026-11-26). Intentional means a reason. Every system-mode line gets one, written beside it (module 7).
One more you’ll see in the guide: an access level that adds a permission set’s permissions to the user’s for a single operation, AccessLevel.withPermissionSetId(). On 2026-11-20 it was “In Developer Preview” (same guide page). Know it exists; don’t build production security on a preview.
Test knowledge. Know that user mode applies the running user’s sharing, object permissions and field-level security, and always respects sharing whatever the class keyword. Know the three forms: WITH USER_MODE or WITH SYSTEM_MODE on queries, as user or as system on DML, and AccessLevel.USER_MODE or SYSTEM_MODE on Database and Search methods, required on the WithBinds methods. Know that WITH SECURITY_ENFORCED doesn’t compile from API 67.0, and why user mode replaced it: polymorphic fields, every clause including WHERE, every FLS error rather than the first, and getInaccessibleFields(). Know getDmlFieldNames() and SaveResult field errors for saves. A likely area, not a known question.
What an architect decides here
Section titled “What an architect decides here”- That every query, save and Database method states its mode, even where user mode is the default.
- That code moving to API 67.0 has every
WITH SECURITY_ENFORCEDreplaced withWITH USER_MODEbefore it’s raised. - How code tells users which field they can’t reach, instead of a bare error.
- Where system mode is the right exception, and the reason written beside each one.
- That nothing in production depends on a Developer Preview access level.
Beyond the exam: what AI changes. Two lessons, and the first is mine from 2023: an AI is a good place to start looking and a bad place to stop. Check its answer against Salesforce’s own pages, the way this course checks every fact. The second: agent actions that query in user mode keep the agent inside the user’s records, objects and fields, and fail with a clear field error instead of quietly leaking. That’s the mode you want in every agent action unless you can write down why not. Chapter 20 covers agents.
Explore
Section titled “Explore”- Set an Access Mode for Database Operations (opens in a new tab), Apex Developer Guide (checked 2026-11-26)
- The WITH SECURITY_ENFORCED SOQL Clause is Removed (opens in a new tab), Salesforce Summer ‘26 Release Notes (checked 2026-11-26)
- Database Operations Run in User Mode by Default, Not System Mode (opens in a new tab), Salesforce Summer ‘26 Release Notes (checked 2026-11-26)
