Siirry sisältöön

Sharing When Writing Apex: Secure Code

Tämä sisältö ei ole vielä saatavilla valitsemallasi kielellä.

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.

  • 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.

Here’s where the chapter starts: the two questions code answers, and the Summer ‘26 change.

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

And where it ends: the review question, and what happens to each answer.

Reviewing code as an architect Reviewing code as an architect. For every class, query, trigger and flow, ask Steve's review question: user mode as security, or system context on purpose? If user mode: the user's records, objects and fields, nothing more to record. If system mode on purpose: a reason beside the line, and one row in the system-mode inventory: what it reaches, why, who it runs for, and who it was tested as. The inventory feeds restriction rules, sharing sets and agents. Footnote: if the answer is "I didn't know," that's the finding. {"author": "Steve Simpson", "blueprint": "1.0.0", "builder": "standard 1.59.0", "built": "2026-11-20", "id": "DGM-SVA-SC-07", "rights": "\u00a9 2026 Twinshark LLC. All rights reserved.", "template": "cells 0.5.0", "theme": "stevetecharc 1.4.0"} THE CODE THE REVIEW Every class, query, trigger and flow every line that reads or writes data User mode, or system on purpose? Steve's review question User mode the user's records, objects and fields; nothing more to record System mode, on purpose a reason beside the line The system-mode inventory what it reaches, why, runs for, tested as; feeds restriction rules, sharing sets, agents ask user mode system mode one row If the answer is "I didn't know," that's the finding. © 2026 Twinshark LLC · stevetecharc.com
Click to enlarge

Seven modules, each standing on its own.

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. 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.

  • 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.

Each module lists its own sources. Three to start with:

Next: Encryption: Classic, Shield and Apex Crypto