Sub topic 13
Criteria-Based Sharing Evaluation and Commit
This chapter covers the last two stages of a save: sharing evaluation and the commit. It assumes you know the earlier steps, from before triggers through after-save flows and roll-up summaries, and that you can read basic Apex.
Criteria-based sharing evaluation
After roll-up summary fields and parent saves finish, Salesforce evaluates criteria-based sharing rules. These rules grant access to records that match field conditions, for example "share every Account where Region is West with the West Sales group."
The evaluation runs late for a reason. It needs the final field values, including changes made by triggers, flows, and workflow field updates. If it ran earlier, a later step could change a field and the access decision would be wrong.
Two points matter in practice:
- Sharing evaluation does not run your triggers or flows. It only updates who can see the record.
- Your Apex code can't hook into this step. You see its effect only after the save, when users gain or lose access.
Commit and post-commit work
If no step raised an unhandled error, Salesforce commits the transaction. Before this point, every change was saved to the database but not permanent. After the commit, the changes are permanent and visible to other users and other transactions.
Post-commit logic runs only after a successful commit. It includes:
- Sending email, such as email alerts and
Messaging.sendEmailcalls. - Starting asynchronous Apex that you queued, such as
@futuremethods, Queueable jobs, and Batch jobs. - Publishing platform events set to publish after commit.
The reason is simple. You should not email a customer about a record that gets rolled back a moment later. Salesforce therefore holds these actions until the data is safe.
Example: queuing work that waits for the commit
public class FollowUpJob implements Queueable {
private Set<Id> accountIds;
public FollowUpJob(Set<Id> accountIds) {
this.accountIds = accountIds;
}
public void execute(QueueableContext ctx) {
List<Task> tasks = new List<Task>();
for (Id accId : accountIds) {
tasks.add(new Task(Subject = 'Call after commit', WhatId = accId));
}
insert tasks;
}
}
trigger AccountQueueFollowUp on Account (after insert) {
System.enqueueJob(new FollowUpJob(Trigger.newMap.keySet()));
}The trigger runs before the commit, but the job starts only after the commit succeeds. If a later step fails and the transaction rolls back, the job never runs.
Common errors
| Mistake | Result |
|---|---|
| Expecting an email to send even though the save fails | No email, because the rollback cancels it |
| Assuming a queued job sees uncommitted data from a failed save | The job never starts |
| Handling an async failure as if it were part of the original save | The original record stays saved |
Exercise
A trigger queues a Queueable job in an after insert trigger. A validation rule is not involved, but a later after-save flow throws an unhandled error. Does the Queueable job run, and is the record saved?
Show answer
No to both. The unhandled error rolls back the whole transaction, so the record is not saved. Because the commit never happened, the queued job never starts.