Ir al contenido

Code Decides Who Sees What

Esta página aún no está disponible en tu idioma.

Module 1 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: You’ve built the whole sharing model in configuration. Then a developer writes a class that queries the same records. Does the code respect what you built?

In class, this is where I say: shifting gears, put on your Apex programmer head. Everything in this course so far is configuration, and an admin can see it in Setup. Code is different. It decides what a user sees just as much as a sharing rule does, and you can’t see that decision without reading the code.

So an architect has to know two things about every piece of code that touches data: which records it lets the user see, and which objects and fields.

Code answers two separate questions, and Apex has a separate setting for each.

Which records? That’s sharing: the org-wide defaults, the role hierarchy, the rules and every other way to share in this course. In Apex it’s the class’s sharing keyword: with sharing, without sharing or inherited sharing. Salesforce: with sharing “enforces org-wide sharing settings and custom sharing rules,” and without sharing “bypasses sharing rules and allows the current user to access all records” (Apex Classes Enforce Sharing Rules by Default (opens in a new tab), Salesforce Summer ‘26 Release Notes, checked 2026-11-26).

When I ask the room what with sharing does to a query, the answer comes back fast: it runs in the user’s context. And without sharing? System context. Without sharing gives you more records; with sharing could give you fewer.

Which objects and fields? That’s chapter 4’s offices: object permissions and field-level security. In Apex it’s the access mode of each database operation: user mode or system mode. “In user mode, database operations enforce the sharing rules, field-level security, and object permissions of the current user” (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).

Notice that user mode answers both questions at once: sharing, objects and fields. That’s why the two settings meet.

So what happens when a class says one thing and a query says another? Salesforce answers it in one note: “If you set a database operation to user mode, the operation respects the user’s sharing rules and overrides the calling class’s sharing declaration.” In system mode, the calling class’s sharing declaration decides (Database Operations Run in User Mode by Default).

Put plainly:

  • A user-mode operation checks everything about the user (records, objects, fields), whatever the class’s keyword says.
  • A system-mode operation skips object and field checks, and leaves the records question to the class’s keyword. In a with sharing class it still sees only the user’s records. In a without sharing class it sees them all.

Here’s that as a sketch (not compiled; chapter 5 module 6 shows every form of the syntax):

public with sharing class OrderReader {
public void read() {
// User mode: this user's records, objects and fields.
List<Order> checked = [SELECT Id, Status FROM Order WITH USER_MODE];
// System mode: no object or field checks; the class's
// "with sharing" still limits it to this user's records.
List<Order> unchecked = [SELECT Id, Status FROM Order WITH SYSTEM_MODE];
}
}

For most of Salesforce’s history, I’d tell a class: our Apex loves to kick into system context. That’s its default behavior, and we need to watch it. A class with no keyword ran without sharing, and a query with no mode ran in system mode. Ten years and more ago, we wrote our own security checks around it, by hand.

Summer ‘26 turned that around. Salesforce calls it “secure by default”:

  • Operations default to user mode. “Apex database operations, such as SOSL and SOQL queries, DML statements, and Database methods, now run in user mode by default.” In earlier API versions they default to system mode, which reaches all data whatever the user’s permissions (Database Operations Run in User Mode by Default).
  • Classes default to with sharing. “Apex classes without an explicit sharing declaration now default to with sharing mode… In earlier API versions, Apex classes without an explicit sharing declaration default to without sharing mode, with some exceptions” (Apex Classes Enforce Sharing Rules by Default).
  • System mode has to be asked for. “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).

Here’s the part an architect has to hold on to. “This versioned change applies to API version 67.0 and later” (same page). Each class and trigger carries the API version it was saved at. Code saved at 66.0 or earlier keeps the old defaults. Code saved at 67.0 or later gets the new ones.

So in any org older than Summer ‘26, there are two worlds side by side, and the same line of code, with no keyword and no mode, means different things in each. That’s why Salesforce’s advice is to stop relying on defaults at all: “To determine the access mode of a database operation regardless of the API version, we recommend that you always set an access mode explicitly,” and for classes, “we recommend that you always include an explicit sharing declaration” (both release notes). Module 5 covers what happens when a team raises a class’s API version, and module 7 makes “explicit, with a reason” a review rule.

Two questions code answers Two questions code answers. Which records: the class's sharing keyword; with sharing uses the user's sharing, without sharing sees every record, inherited sharing takes the caller's. Which objects and fields: each operation's access mode; user mode checks objects, fields and sharing, system mode checks none of them; user mode overrides the keyword, and system mode leaves the records question to the keyword. From API version 67.0, secure by default: classes default to with sharing, operations default to user mode, system mode must be asked for, and older code keeps the old defaults. Footnote: never rely on the default; write the keyword and the mode, and the reason for any exception. {"author": "Steve Simpson", "blueprint": "1.0.0", "builder": "standard 1.59.0", "built": "2026-11-20", "id": "DGM-SVA-SC-01", "rights": "\u00a9 2026 Twinshark LLC. All rights reserved.", "template": "comparison 0.5.0", "theme": "stevetecharc 1.4.0"} Which records? the class's sharing keyword with sharing: the user's sharing without sharing: every record inherited sharing: the caller's Which objects and fields? each operation's access mode User mode: objects, fields and sharing System mode: none of them User mode overrides the keyword System mode leaves records to the keyword From API 67.0 secure by default Classes default to with sharing Operations default to user mode System mode must be asked for Older code keeps the old defaults Never rely on the default: write the keyword and the mode, and the reason for any exception. © 2026 Twinshark LLC · stevetecharc.com
Click to enlarge

Test knowledge. Know the two settings: the class’s sharing keyword decides which records (with sharing enforces sharing; without sharing bypasses it), and the operation’s access mode decides objects and fields (user mode enforces sharing, object permissions and field-level security; system mode doesn’t). Know that a user-mode operation overrides the class’s keyword, and a system-mode operation leaves record access to the keyword. Know that from API version 67.0 (Summer ‘26), classes with no keyword default to with sharing and database operations to user mode, that the change is versioned, and that Salesforce recommends always writing the keyword and the mode explicitly. A likely area, not a known question.

  • Which code in the org touches data users see, and who reads it with security in mind.
  • That every class states its keyword and every database operation states its mode, whatever its API version.
  • Where system mode or without sharing is genuinely needed, and the reason, written next to it.
  • How the org’s mix of old and new API versions is tracked, so nobody is surprised by a default.

Beyond the exam: what AI changes. Agents call Apex as actions, and the action’s code decides what the agent can read, by the same two settings. An action in a without sharing class, or with a system-mode query, hands the agent records the user couldn’t see. Before an Apex action goes into an agent, read its keyword and its modes as carefully as you’d read a sharing rule. Chapter 20 covers agents.