Sub topic 14
Criteria-based sharing evaluation
This chapter assumes you know how sharing rules grant record access, and that you have read the earlier chapters on after-save flows and roll-up summary fields. Criteria-based sharing evaluation is the step that runs after roll-up summary fields and parent record saves, and just before the commit.
What the step does
A criteria-based sharing rule shares records with a public group, role, or territory when field values match criteria. For example, "share Opportunities where Region is EMEA with the EMEA Sales group."
Salesforce decides whether each saved record matches such a rule near the end of the save. If the record matches, Salesforce grants access by creating share rows. If a record that used to match no longer does, Salesforce removes the access that rule granted.
Two points follow from this position in the sequence:
- Evaluation uses the final field values. Before triggers, before-save flows, after triggers, workflow field updates, and after-save flows have all had their chance to change the record.
- Evaluation happens inside the transaction. If anything fails before the commit, the sharing changes roll back with everything else.
Where it sits in the sequence
Why the timing matters
Criteria fields set by your own automation
Your automation often sets the field that a sharing rule tests. Because evaluation comes last, the rule sees the value your automation wrote, not the value the user typed.
This trigger derives a region from a country, and a criteria-based rule tests Region__c. The field is hypothetical, so adapt the names to your org.
trigger OpportunityRegion on Opportunity (before insert, before update) {
for (Opportunity o : Trigger.new) {
if (o.Country__c == 'Germany' || o.Country__c == 'France') {
o.Region__c = 'EMEA';
}
}
}The before trigger changes Region__c with no DML. When the record is saved, the sharing step sees EMEA and grants access to the EMEA group. A user who edits only Country__c still triggers the sharing change.
Your code cannot see the new access yet
Triggers and flows run before this step. When an after trigger runs, the criteria-based share rows for the current save do not exist yet.
- A query in an after trigger that runs with sharing may not return records that become visible only through this save.
- Code that checks "can the EMEA group see this record?" during the save gets the old answer.
- Post-commit work, such as asynchronous Apex you enqueue, runs later and can see the finished result.
If logic depends on the new access, move it to an asynchronous job. Queueable Apex works well for this.
No triggers fire on share rows
Creating share rows through criteria-based rules does not fire Apex triggers on the object. Inserting a row into a share object with Apex, which is called Apex managed sharing, is different. That is ordinary DML and belongs in an after trigger, because it needs the record Id.
| Aspect | Criteria-based sharing rule | Apex managed sharing |
|---|---|---|
| Who creates the share rows | Salesforce, in the sharing evaluation step | Your code, in an after trigger or other Apex |
| Timing in the save | After roll-up summaries, before commit | Whenever your code runs |
| Needs the record Id | No | Yes |
| Counts against DML limits | No | Yes, one statement for the share rows |
| Re-evaluated when criteria fields change | Automatically | Only if your code does it |
Edge cases
- Owner changes. A new owner can change which owner-based rules and role-hierarchy access apply. This recalculation follows the same pattern.
- Removed access. If a field update means a record no longer meets the criteria, access granted by that rule is removed. Users who lose access can see "record not found" errors in the UI immediately after the save.
- Large recalculations. Changing the rule itself, or group and role membership, can recalculate sharing for many records. Salesforce often handles that outside your save, so do not assume it completes within a single transaction.
- Rollback. If a later validation or unhandled exception ends the transaction, the sharing evaluation for that record is undone with everything else.
Testing the outcome
A test cannot observe the evaluation mid-transaction, but it can verify the result after the transaction. This test assumes an EMEA public group and a criteria-based rule already exist in the org, since sharing rules are metadata and are not created in Apex tests.
@IsTest
private class OpportunityRegionTest {
@IsTest
static void regionIsDerivedBeforeSave() {
Opportunity o = new Opportunity(
Name = 'Test',
StageName = 'Prospecting',
CloseDate = Date.today().addDays(30),
Country__c = 'Germany'
);
insert o;
Opportunity saved = [SELECT Region__c FROM Opportunity WHERE Id = :o.Id];
System.assertEquals('EMEA', saved.Region__c);
}
}The assertion proves the criteria field holds the value the sharing step will evaluate. Whether the group can then see the record is an org-configuration check, not something this test proves.
Exercise
A criteria-based rule shares Opportunities with Region_c = 'EMEA' with a group. A before trigger sets Regionc from Country_c. An after trigger queries, with sharing, for the records the EMEA group can see. Does that query include the Opportunity being inserted, and why?
Show answer
Probably not. The before trigger makes the value correct before the save, so the rule will match. But sharing evaluation runs after the after triggers, so the share row does not exist yet when the after trigger runs. Move any logic that needs the new access to asynchronous code that runs after the commit.
Exercise
A validation rule fails after a record would have matched a sharing rule. Is the access granted?
Show answer
No. The error rolls back the whole transaction, and the sharing evaluation either never runs or is undone, so no access changes are saved.