Sub topic 3
Criteria-Based Sharing Evaluation
This chapter assumes you know basic Apex triggers, Salesforce records, declarative automation, and DML operations. It focuses on what happens after the record has been saved but before the transaction finishes.
Sharing, commit, and rollback
After after-save flows and roll-up summary updates finish, Salesforce evaluates criteria-based sharing rules. These rules grant record access when field values match criteria. They do not usually change the saved record.
Salesforce then commits the transaction. Commit makes the record and related changes permanent. Post-commit work can include sending email, publishing outbound messages, and running asynchronous automation.
Shared limits and re-entry
Governor limits apply across the transaction. For example, one transaction shares limits for SOQL queries and DML statements across triggers, flows, and other automation. Bulkify logic by processing collections and moving queries outside loops.
Automation can also re-enter an object. A workflow field update, flow, or related record update can cause an update trigger to run again. Use a transaction-scoped guard when the operation must happen only once.
public class TriggerGuard {
public static Set<Id> processed = new Set<Id>();
}
trigger AccountAfterUpdate on Account (after update) {
for (Account a : Trigger.new) {
if (TriggerGuard.processed.add(a.Id)) {
System.debug('Processing ' + a.Id);
}
}
}Ordering and debugging
Multiple triggers on one object can run in an order you should not rely on. Prefer one trigger entry point that delegates to ordered handler methods. Before-save flows and after-save flows also participate in the same transaction, so document dependencies instead of assuming an order between unrelated automations.
Use debug logs to confirm the actual behavior. Add distinctive messages such as BEFORE, AFTER, and POST PROCESS, then inspect timestamps, execution units, and repeated trigger entries.
Where to put logic
Use a before trigger or before-save flow to change fields on the record being saved. Use an after trigger when you need the record Id or must create related records. Use Flow for maintainable declarative logic, and Apex when the logic needs complex processing, reusable code, or precise bulk behavior.
Exercise
Why might an after update trigger appear twice in one save?
Show answer
A workflow field update or other automation may update the record again, causing trigger re-entry in the same transaction.