Bỏ qua để đến nội dung

Triggers and the API 67.0 Upgrade

Nội dung này hiện chưa có sẵn bằng ngôn ngữ của bạn.

Module 5 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: A trigger can’t have a sharing keyword. What changed inside triggers at API 67.0, and what can happen when a team raises a class or trigger to the new version?

Triggers are the code nobody calls on purpose. A user saves a record, an integration loads a thousand, a site user places an order, and the trigger runs, for all of them. That’s why they matter so much here. And Summer ‘26 changed them in a way that’s easy to miss, because the trigger itself didn’t change at all.

“Apex triggers now always run in a without sharing context. Previously, nested triggers enforced sharing rules in certain edge cases” (Apex Triggers Always Run in a “without sharing” Context (opens in a new tab), Salesforce Summer ‘26 Release Notes, checked 2026-11-26). And you can’t change it: “Apex triggers can’t have an explicit sharing declaration” (Use the with sharing, without sharing, and inherited sharing Keywords (opens in a new tab), Apex Developer Guide, checked 2026-11-26).

That part applies in every API version.

Here’s the change. “In API version 67.0 and later, database operations within trigger bodies, including SOQL queries, SOSL queries, DML statements, and Database methods, run in user mode unless system mode is explicitly specified.” User mode overrides the trigger’s without sharing context, so the body effectively runs with sharing (Apex Triggers Always Run in a “without sharing” Context).

So a trigger is without sharing on the outside and, at 67.0, effectively with sharing on the inside, unless the code says otherwise. Salesforce’s own example puts the two queries side by side: one with no mode, which “defaults to WITH USER_MODE” and returns the contacts the user can see; and one with WITH SYSTEM_MODE, where “Record sharing remains governed by the trigger’s own without sharing context, so all records are visible regardless of the user running the query” (same page).

Read that against the triggers you already have. A trigger is written to do something on the user’s behalf, often to records the user can’t see:

  • A roll-up. A restaurant’s staff save an order, and a trigger totals every order on the account. At 66.0, the query sees every order. Raised to 67.0 with no mode, it sees only the orders this user can see, and the total is quietly wrong.
  • An update to a related record. The trigger stamps a date on the account, which a site user can only read. At 67.0, in user mode, that save can fail.
  • Share logic. Chapter 11’s record trigger and user trigger write share rows for other people (chapter 11, module 5). Code like that exists precisely to do what the user can’t. Don’t assume it still works in user mode; test it.

None of these are bugs in Salesforce. They’re the secure default doing what it says. The risk is a team raising a version to get a new feature, and finding out in production.

Here’s the roll-up done on purpose, as a sketch (not compiled):

trigger OrderTotals on Order (after insert, after update) {
OrderTotalsHandler.recalculate(Trigger.new);
}
public without sharing class OrderTotalsHandler {
// Reason for without sharing and system mode: the account total must
// count every order on the account, not only those this user can see.
public static void recalculate(List<Order> changed) {
Set<Id> accountIds = new Set<Id>();
for (Order o : changed) { accountIds.add(o.AccountId); }
List<AggregateResult> totals = [SELECT AccountId, SUM(TotalAmount) amount
FROM Order WHERE AccountId IN :accountIds
WITH SYSTEM_MODE GROUP BY AccountId];
// ...write the totals to the accounts, as system, with the same reason
}
}

Two habits are visible there: the logic lives in a handler, and the mode is explicit with its reason beside it. Both are Salesforce’s own advice: “To control behavior reliably independent of the API version, set explicit access mode on all database operations in your trigger and handler classes. When possible, delegate all business logic to trigger handlers” (Apex Triggers Always Run in a “without sharing” Context).

API versions are per class and per trigger, so an org doesn’t upgrade all at once. Somebody raises one, usually to use something new. Here’s the plan I’d ask for before anyone does:

  1. Know what you have. List every class and trigger with its API version, and every one that touches data.
  2. Remove what won’t compile. “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). Replace it with WITH USER_MODE.
  3. Make the old behavior explicit first. Add a sharing keyword to every class and a mode to every database operation, matching what it does today. Then raising the version changes nothing, because nothing relies on a default.
  4. Decide each exception on purpose. Where the old code ran in system mode only by default, ask whether it should. Many queries should become user mode. The ones that must stay system mode get a reason.
  5. Test as real users. Not as an administrator. As a sales user, a site user, the integration user, with System.runAs in tests (chapter 13, module 3): one who should see the result, one who shouldn’t, and check the roll-ups and related saves come out right for both.

Step 3 is the one that matters most. Once every class says its keyword and every operation says its mode, the API version stops being a security question.

A trigger and the API 67.0 change A trigger and the API 67.0 change. The trigger: always without sharing, in every version; it can't take a keyword. It delegates to a handler class, with your keyword. The handler runs each database operation: with no mode, user mode from API 67.0, so it sees only the user's records; with WITH SYSTEM_MODE, every record, under the trigger's without sharing. Before you raise the version: list classes and triggers by version, replace WITH SECURITY_ENFORCED, write every keyword and mode first, and test as real users. Footnote: once every class says its keyword and every operation its mode, the API version stops being a security question. {"author": "Steve Simpson", "blueprint": "1.0.0", "builder": "standard 1.59.0", "built": "2026-11-20", "id": "DGM-SVA-SC-05", "rights": "\u00a9 2026 Twinshark LLC. All rights reserved.", "template": "cells 0.5.0", "theme": "stevetecharc 1.4.0"} THE TRIGGER WHAT IT RUNS Trigger: always without sharing every version; no keyword allowed Handler class: your keyword delegate the logic here Each database operation no mode: user mode from 67.0; WITH SYSTEM_MODE: every record Before you raise the version list versions, replace SECURITY_ENFORCED, write every mode, test as real users delegates runs Once every class says its keyword and every operation its mode, the API version stops being a security question. © 2026 Twinshark LLC · stevetecharc.com
Click to enlarge

Test knowledge. Know that triggers always run without sharing and can’t declare a keyword; that from API 67.0 the database operations in a trigger body run in user mode unless system mode is explicit, effectively enforcing with sharing inside the trigger; and that WITH SYSTEM_MODE in a trigger sees all records under the trigger’s without sharing context. Know Salesforce’s advice: explicit access modes on every operation in triggers and handlers, and business logic delegated to handlers. Know that classes using WITH SECURITY_ENFORCED don’t compile at 67.0. A likely area, not a known question.

  • Who may raise a class or trigger’s API version, and that it goes through the plan, not a quick edit.
  • That every trigger delegates to a handler with an explicit keyword, and every operation states its mode.
  • Which trigger work must see more than the user (roll-ups, related updates, share logic), and the reason written beside each system-mode line.
  • The users the upgrade is tested as: the least-privileged ones, the site users, the integration user.

Beyond the exam: what AI changes. Agents save records too, and every save fires the triggers. At 67.0, a trigger with no explicit mode runs its queries as the agent’s user. If that user has narrower access than the people who usually save those records, totals and related updates can come out differently for the agent. Put the agent’s user on the upgrade test list. Chapter 20 covers agents.