Reviewing Code as an Architect
هذا المحتوى غير متوفر بلغتك بعد.
Module 7 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: Every module in this chapter ended with a decision. How does an architect check those decisions were kept, in code and flows someone else wrote?
When I review a flow, the loops get my attention first. That’s where the performance problems hide, so it’s one of the first things I look at in anybody’s work product. For sharing, the same instinct points somewhere else: every line that reads or writes data, and every place the code decides to see more than the user can.
You don’t need to read every line of every class to do this well. You need one question, a short checklist, and one list that the whole project keeps.
One question for every database line
Section titled “One question for every database line”Here’s the question I gave my class in 2023, and it’s still the one I ask: when there’s Apex code with a query or a save, are they using user mode as security, or are they consciously disregarding that and going as system context?
Both answers can be right. User mode is the secure default (module 3). System mode is fine when it’s a decision: a roll-up that must count every order, a check across every restaurant’s deliveries. What’s never right is the third answer: “I didn’t know.” If nobody can say why a line runs in system mode, that’s the finding.
Since Summer ‘26 the default answer is user mode at API 67.0 and later (module 1). That makes the question easier to ask, not unnecessary. Older classes still run in system mode by default, and newer code can still say without sharing, WITH SYSTEM_MODE or as system in one word.
The checklist
Section titled “The checklist”Seven checks, one for each thing this chapter taught. Each points back to the module that explains it.
- Every class says its keyword (module 2). Entry points say
with sharingunless there’s a reason; shared service code saysinherited sharing;without sharingappears only for targeted elevation. - Every database operation says its mode (module 3).
WITH USER_MODEorWITH SYSTEM_MODE,as useroras system, anAccessLevelon Database methods. Nothing relies on the API version’s default. - Every exception has its reason beside it. Each
without sharing,WITH SYSTEM_MODE,as systemandAccessLevel.SYSTEM_MODEhas a comment that says why, and a row in the inventory below. - Fields are failed or stripped on purpose (module 4). The fields a process needs fail when missing; the extras are stripped; anything built from a request, a screen or an integration is stripped for its operation before it’s saved.
- Triggers delegate, and version raises follow the plan (module 5). Logic in handlers with explicit modes; nobody raises a class or trigger to API 67.0 as a quick edit.
- Flows record their run context (module 6). Screen flows on sites run as the user; every System Context without Sharing flow is listed; flows that a system-context caller mustn’t widen use User Context—Enforces User Permissions.
- Tests run as real users (chapter 13, module 3). One user who should get the result and one who shouldn’t, not an administrator.
The first two checks are the cheap ones, and they make all the others possible. Once every class and every operation says what it does, a reviewer is reading decisions instead of guessing at defaults.
Write the exception down
Section titled “Write the exception down”An exception without a reason is just a hole that nobody’s looked at yet. So the reason goes in the code, right beside the line, where the next developer and the next reviewer will see it. Modules 2, 4 and 5 already did this. Here’s module 2’s slot counter with the comment in the shape I’d ask for, as a sketch (not compiled):
public without sharing class SlotCounter { // SYSTEM MODE: counts every restaurant's orders in a slot; returns a number, never a record. // Inventory row: SM-02. Agreed: data owner for Orders. public static final Integer CAPACITY = 12; public static Integer countBooked(Datetime slot) { return [SELECT COUNT() FROM Order WHERE Delivery_Slot__c = :slot WITH SYSTEM_MODE]; }}Three things in that comment: what it reaches that the user can’t, what it gives back, and where it’s recorded. The last one matters most, because a comment lives in one file and the inventory lives with the whole project.
Salesforce’s own scanner asks for the same habit. When you suppress a Code Analyzer finding, its guidance is to “Document why you are suppressing,” and the configuration has a reason for each suppression, “optional but recommended” (Suppress Violations (opens in a new tab), Salesforce Code Analyzer, checked 2026-11-26). Make it required on your project.
The system-mode inventory
Section titled “The system-mode inventory”The inventory is one list of everything that runs with more access than its user: classes, queries, trigger handlers and flows. It’s short on a healthy project. If it’s long, that’s worth knowing too.
On the restaurant ordering site, it might look like this:
| Row | What | Kind | Reaches beyond the user | Why | Runs for | Tested as |
|---|---|---|---|---|---|---|
| SM-01 | OrderTotalsHandler | Trigger handler, without sharing, system mode | Every order on the account | The account total must count all of them | Staff, site users, the integration user | A site user and a staff user |
| SM-02 | SlotCounter | Class, without sharing, system mode | Every restaurant’s orders in a slot | Returns a count, never a record | Site users | A site user who has no orders in the slot |
| SM-03 | OrderTable.rows() | Query, system mode, then stripped | Every field on the user’s orders | Strips per user; records still with sharing | Staff | A user without the internal-cost field |
| SM-04 | Slot-count subflow | Flow, System Context without Sharing | Every order in a slot | The one elevated step behind the booking screen | Site users, including guests | A guest and a site user |
Each column earns its place. Reaches beyond the user is what you’d lose if it leaked. Runs for tells you whether guests or site users are in the blast radius. Tested as tells you someone proved it.
And the inventory is what the rest of Part 3 needs from this chapter:
- Restriction rules (chapter 16, module 7). “Restriction rules aren’t applied for code executed in System Mode” (Restriction Rule Considerations, as cited there). Every row on this list is a place a restriction rule doesn’t reach.
- Sharing sets and share groups (chapter 15). Site users get their records through these. Any row that runs for site users can reach past them.
- Agents (chapter 20). An agent action is often a class or a flow. The first question for any agent action is whether it’s on this list.
What tools can find, and what they can’t
Section titled “What tools can find, and what they can’t”Use a scanner. Salesforce Code Analyzer “automatically scans and evaluates Apex, Visualforce, Flows, and Lightning components,” and its Flow Scanner engine “audits Salesforce Flows and reports detailed information about security issues” (Overview of Salesforce Code Analyzer (opens in a new tab), Salesforce Code Analyzer, checked 2026-11-26). It runs from the command line, in VS Code and in a pipeline, so it can check every change before a person looks.
Two of the PMD rules it runs are about this chapter (Apex Security rules (opens in a new tab), PMD documentation, checked 2026-11-26):
- ApexSharingViolations detects “classes declared without explicit sharing mode if DML methods are used.” That’s check 1.
- ApexCRUDViolation “validates you are checking for access permissions before a SOQL/SOSL/DML operation,” and “When using user mode, no violation is reported by this rule.” That’s checks 2 and 4.
Now notice what those rules can’t do. ApexSharingViolations finds a class with no keyword. A class that says without sharing has a keyword, so it passes. The scanner can tell you a decision is missing; it can’t tell you a decision is wrong. Whether SlotCounter should see every order is a business question, and that’s the reviewer’s job.
So the scanner does the first two checks on every change, and the person does the rest: reads every exception, checks its reason, and keeps the inventory true.
Test knowledge. Know what to look for when reviewing code for sharing and access: an explicit sharing keyword on every class (with sharing by default, inherited sharing for shared code, without sharing only for targeted elevation), an explicit access mode on every database operation, a reason for every use of system mode, fields stripped or checked, logic delegated from triggers, flow run context set deliberately, and tests run as users with and without access. Know that restriction rules aren’t applied to code running in system mode, and that a static scanner can find a missing sharing declaration but can’t judge whether without sharing is justified. A likely area, not a known question.
What an architect decides here
Section titled “What an architect decides here”- That the review question is asked of every database line, and “I didn’t know” is treated as a finding.
- The checklist the project reviews against, and which checks the scanner runs on every change.
- The format of the reason comment, and that a suppressed scanner finding needs a reason too.
- Who owns the system-mode inventory, who may add a row, and who must agree to it (the data owner, the security lead).
- That every row says who it runs for and who it was tested as, and that the inventory goes to the restriction rule, site and agent designs.
Beyond the exam: what AI changes. More and more code is written with an AI’s help, and AI will happily write
without sharingorWITH SYSTEM_MODEto make an error go away. That makes the review more important, not less. At Dreamforce ‘26 I put it this way: code reviews and security validation are the things that don’t change. AI can generate code. It cannot accept responsibility. The reason beside the line and the row in the inventory are how a person accepts it. And when the code is an agent action, the inventory row tells you what the agent can reach. Chapter 20 covers agents.
Explore
Section titled “Explore”- Overview of Salesforce Code Analyzer (opens in a new tab), Salesforce Code Analyzer (checked 2026-11-26)
- Suppress Violations (opens in a new tab), Salesforce Code Analyzer (checked 2026-11-26)
- Apex Security rules (opens in a new tab), PMD documentation (checked 2026-11-26)
- Restriction Rule Considerations (opens in a new tab), Salesforce Help (checked 2026-11-26)
