Sub topic 12
Sharing recalculation and what triggers it
This chapter assumes you know how org-wide defaults, the role hierarchy, public groups, and owner-based and criteria-based sharing rules work. It also assumes you have read the earlier chapters on manual sharing and account teams. Here you will learn when Salesforce rebuilds access, why the rebuild is not instant, and what to do when it is slow or fails.
What recalculation is
Salesforce does not decide access by checking every rule each time a user opens a record. It stores the results. Owner access, sharing rule access, team access, and manual access are written as rows in share tables. Access from the role hierarchy is held in internal structures that Salesforce maintains for you.
Sharing recalculation is the process that rebuilds those stored results after something changes. It adds rows that should now exist and removes rows that should no longer exist. Because it works on stored results, a sharing change takes effect only once recalculation has finished.
What triggers recalculation
Some changes affect a single record. Others affect the whole configuration for an object, and these can touch millions of rows.
| Change | Scope of recalculation |
|---|---|
| Create, edit, or delete a sharing rule | All records of that object that the rule covers |
| Change a rule's target group or access level | All records the rule matches |
| Change an org-wide default | The whole object, plus dependent objects |
| Turn **Grant Access Using Hierarchies** on or off | The whole object |
| Move a role or change a user's role | Records owned by users in the affected branch, and the rules that target those roles |
| Add or remove members of a public group, queue, or role-based target | Every rule that uses that group |
| Change a record's owner | That record, plus related child records |
| Edit a field that a criteria-based rule tests | That record only |
The last two rows are cheap. They run per record, so they feel immediate. The first rows are configuration changes. They are queued as background jobs, and their duration depends on the amount of data and the number of rules.
What does not trigger recalculation
- Changing object permissions or field-level security. These are not record sharing, so no share rows change.
- Editing a field that no rule or team uses. The record's access is unaffected.
- Adding an account team member or inserting a manual share. These write one share row directly. Nothing needs rebuilding.
How recalculation runs
Recalculation is asynchronous. When you save a sharing change, Salesforce queues a job and returns control to you. The setup page shows that a recalculation is in progress, and Salesforce emails you when it finishes. Further sharing configuration changes may be blocked or delayed until the job completes.
During that window, access is in a mixed state. Users who should gain access may not have it yet. Users who should lose access may keep it for a short time. Plan large sharing changes for quiet hours, and do not tell stakeholders a change is live until the job has finished.
Watching recalculation with SOQL
You can check the effect of a rule change by querying the share table before and after. Rows created by sharing rules carry the row cause Rule, so they are easy to isolate. Start with a count:
SELECT COUNT()
FROM Invoice__Share
WHERE RowCause = 'Rule'Then inspect a single record, the same way you did in the share tables chapter:
SELECT Id, ParentId, UserOrGroupId, AccessLevel, RowCause
FROM Invoice__Share
WHERE ParentId = '<invoice record Id>'Suppose you deploy this criteria-based rule, which gives the Finance_Managers group read access to overdue invoices:
<?xml version="1.0" encoding="UTF-8"?>
<SharingRules xmlns="http://soap.sforce.com/2006/04/metadata">
<sharingCriteriaRules>
<fullName>Share_Overdue_Invoices</fullName>
<accessLevel>Read</accessLevel>
<label>Share Overdue Invoices</label>
<sharedTo>
<group>Finance_Managers</group>
</sharedTo>
<criteriaItems>
<field>Status__c</field>
<operation>equals</operation>
<value>Overdue</value>
</criteriaItems>
</sharingCriteriaRules>
</SharingRules>Right after the deployment, the count query may still return the old number. Once the job finishes, it rises by the number of overdue invoices, and each affected record has a Rule row for the group. Later, when a user changes an invoice's Status__c from Overdue to Paid, that record is re-evaluated at once. Its Rule row disappears without any bulk job.
What recalculation removes
Recalculation can delete access, so know what it removes:
- Rule shares. If a record no longer matches a rule, or the rule is deleted, its
Rulerows are removed. - Manual shares. When a record's owner changes, manual shares on that record are removed. This is standard behavior, and a recalculation is not needed to cause it.
- Team shares. Team access follows team membership. It is not rebuilt by rule recalculation.
- Redundant shares. If you widen an org-wide default, for example from Private to Public Read Only, share rows that grant no extra access can be cleaned up.
Shares created in Apex with a custom Apex sharing reason are different. They survive an owner change, which is why programmatic sharing can persist where manual sharing does not. A later chapter covers this.
Manual recalculation and deferring it
The sharing rules related list on the Sharing Settings page has a Recalculate button for each object. Use it when a rule update failed partway through, or when you suspect rows are out of date. Do not use it routinely. A healthy org does not need it.
For large data volumes, Salesforce offers deferred sharing calculations. Support must enable this feature for your org. Once it is on, an administrator can suspend group membership calculations and sharing rule calculations, make many changes, and then resume. The calculations then run once instead of after every edit. While suspended, new group members and new rules have no effect, so schedule the work and always resume afterward.
Common errors and failure modes
- Row lock errors. Loading many records, or changing group membership while a recalculation is running, can fail with
UNABLE_TO_LOCK_ROW. Retry later, and avoid parallel loads against the same parent records or groups. - Long-running jobs. Many rules, deep role hierarchies, and objects with millions of records make recalculation slow. Every extra rule costs time on every configuration change.
- Premature testing. Testing as a user right after saving a rule may show the old access. Wait for the completion email.
- Ownership skew. One user who owns a very large number of records makes ownership changes and role moves expensive, because the job must rebuild access for all of them.
- Assuming a stale hierarchy. Moving a role in the hierarchy triggers recalculation for the whole branch. Do it outside business hours.
Quick checklist
- Identify whether the change is per record or configuration-wide.
- For configuration-wide changes, schedule them off-hours and expect a queue.
- Save once, then wait for the completion email.
- Verify with a share table query, then with the Login As feature or a test user.
- If a rule update failed, use Recalculate on that object's rules.
Exercise
An administrator edits a criteria-based sharing rule on Invoice__c at 9:00 and tests as a Finance user at 9:02. The user still cannot see an invoice that matches the rule. Name two possible explanations and one SOQL check.
Show answer
The recalculation job may still be running, because rule changes are processed asynchronously. The rule may also have failed to apply, in which case the Recalculate button on that object's rules is needed. To check, query Invoice_Share for the invoice's ParentId and look for a row with RowCause = 'Rule' for the FinanceManagers group. If the row is missing, access has not been rebuilt yet.
Exercise
A user changes an invoice's Status__c from Overdue to Paid, and a criteria-based rule shares only Overdue invoices. Does this need a full recalculation of the Invoice object? What happens to the rule's share row?
Show answer
No. A change to a field the rule tests re-evaluates only that record, so no object-wide job is queued. The record no longer matches, so its Rule share row is removed.