Sharing When Writing Apex: Secure Code
Deze inhoud is nog niet vertaald.
Chapter 17 of Securing Salesforce: Sharing and Visibility, Part 3: Beyond the Company and Beyond the Record. New to the course? Start with chapter 1.
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.
For most of Salesforce’s history, our Apex loved to kick into system context. That was its default behavior, and we had to watch it. Summer ‘26 turned that around: from API version 67.0, classes default to with sharing and database operations to user mode. Secure by default, but only for code saved at the new version, so every org older than Summer ‘26 now has two worlds side by side.
This chapter is about both worlds. It starts with the two questions code answers, walks through the tools for each, covers triggers and flows, the places that are easiest to miss, and ends with how an architect checks it all: one question for every database line, and one list of everything that runs in system mode.
The ideas in this chapter
Section titled “The ideas in this chapter”- Two questions, two settings. The class’s sharing keyword decides which records; each operation’s access mode decides objects and fields. User mode checks all three; system mode leaves records to the keyword.
- Think in entry points. With sharing at the top of the call stack, inherited sharing for shared code, without sharing only as a narrow elevation that returns answers, not records.
- User mode checks more, and tells you more. It replaced WITH SECURITY_ENFORCED: polymorphic fields, every clause including WHERE, every field error.
- Fail, leave it out, or ask first. User mode fails and names the field; stripInaccessible leaves fields out and cleans untrusted input; describe calls check before you start.
- Triggers: without sharing outside, user mode inside. From API 67.0, a trigger’s queries run as the user unless the code says otherwise, so make every mode explicit before raising a version.
- Flows have a run context too. It depends how the flow starts; screen flows should run as the user, with any elevation in one small subflow.
- Every exception has a reason and a row. Ask of every line: user mode as security, or system context on purpose? Keep the inventory of what runs in system mode.
The picture
Section titled “The picture”Here’s where the chapter starts: the two questions code answers, and the Summer ‘26 change.
And where it ends: the review question, and what happens to each answer.
The path through this chapter
Section titled “The path through this chapter”Seven modules, each standing on its own.
- Code Decides Who Sees What. Two questions, two settings; where they meet; the old default and the new one; versioned, so your org has both.
- with, without and inherited sharing: Think in Entry Points. The three keywords; entry points; shared code; does it flip back and forth; narrow elevation; the defaults, and why not to rely on them.
- User Mode, and Why WITH SECURITY_ENFORCED Went Away. What user mode checks; the three ways to ask for it; the four reasons the old clause went; the field it names when it says no; system mode as the exception you write down.
- Checking Permissions Yourself: stripInaccessible and Describe Calls. Fail or leave it out; stripInaccessible for results, saves and untrusted input; describe calls to ask first; what none of them enforce.
- Triggers and the API 67.0 Upgrade. Always without sharing outside; user mode inside from 67.0; why it catches teams; a five-step plan for raising the version.
- Flows: Run Context, and Who May Run Them. The default by launch method; the four run options and Winter ‘27’s new one; Salesforce’s data-safety advice; Run Flows and per-flow access.
- Reviewing Code as an Architect. One question for every database line; the checklist; the reason beside every exception; the system-mode inventory; what scanners can and can’t decide.
Then check what stuck with the study guide.
What an architect decides here
Section titled “What an architect decides here”- That every class states its keyword and every database operation its mode, whatever its API version, so nothing relies on a default.
- Where system mode or without sharing is genuinely needed, kept narrow, returning answers rather than records, with the reason written beside it.
- For each screen, integration and action, which fields must be there (fail) and which are extras (strip), and that untrusted input is stripped before it’s saved.
- Who may raise a class or trigger’s API version, and the plan it follows.
- Each flow’s run context, and why, with screen flows on sites running as the user.
- Who owns the system-mode inventory, and that the restriction rule, site and agent designs read it.
Beyond the exam: what AI changes. Agents call Apex and flows as actions, and the action’s code decides what the agent can read, by the same settings this chapter teaches. And more code is now written with an AI’s help, which will happily write without sharing or WITH SYSTEM_MODE to make an error go away. As I put it at Dreamforce ‘26: AI can generate code. It cannot accept responsibility. The reason beside the line and the row in the inventory are how a person does. Chapter 20 covers agents.
Explore
Section titled “Explore”Each module lists its own sources. Three to start with:
- 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)
- Use the with sharing, without sharing, and inherited sharing Keywords (opens in a new tab), Apex Developer Guide (checked 2026-11-26)
- Flow Run Context (opens in a new tab), Salesforce Help (checked 2026-11-26)
