You are here:
Recalculate Apex Managed Sharing
Developers can write batch Apex classes that recalculate the Apex managed sharing for a specific custom object.
Required Editions
| Available in: Salesforce Classic |
| Available in: Professional, Enterprise, Performance, Unlimited, Developer, and Database.com Editions |
| User Permissions Needed | |
|---|---|
| To associate an Apex managed sharing recalculation class: | Author Apex |
| To run an Apex managed sharing recalculation: | Author Apex OR Manage Sharing |
Developers can write batch Apex classes that recalculate the Apex managed sharing for a specific custom object. You can associate these classes with a custom object on its detail page, and execute them if a locking issue prevents Apex from granting access to a user as defined by the application’s logic. Apex sharing recalculations are also useful for resolving visibility issues due to coding errors. For example, if a developer corrects a coding error that prevented users from accessing records they should see, the correction might only affect records created after the code update. To ensure the correction applies to existing records as well, the developer can run an Apex sharing recalculation to validate sharing on all records.
You can run Apex sharing recalculations from a custom object's detail page. You can
also run them programmatically using the Database.executeBatch method. In addition, Salesforce automatically
runs Apex recalculation classes defined for a custom object every time a custom
object's organization wide sharing default access level is updated.
Salesforce automatically recalculates sharing for all records on an object when its organization-wide sharing default access level changes. The recalculation includes access granted by sharing rules. In addition, all types of sharing are removed if the access they grant is redundant. For example, the manual sharing which grants Read Only access to a user is deleted when the object’s sharing model is changed from Private to Public Read Only.
- From the management settings for the custom object, go to Apex Sharing Recalculations.
-
Choose the Apex class that recalculates the Apex sharing for this object. The
class you choose must implement the
Database.Batchableinterface. You cannot associate the same Apex class multiple times with the same custom object. - Click Save.
For information on creating Apex managed sharing and recalculation classes, see the Apex Developer Guide.
To associate an Apex managed sharing recalculation class with a custom object:
To run an Apex sharing recalculation, from the management settings for a custom object, go to Apex Sharing Recalculation, and then click New.
When working with Apex sharing recalculations, note the following.
- The Apex code that extends the sharing recalculation can process a maximum of five million records. If this Apex code affects more than five million records, the job fails immediately.
- You can monitor the status of Apex sharing recalculations in the Apex job queue.
- You can associate a maximum of five Apex sharing recalculations per custom object.
- You cannot associate Apex sharing recalculations with standard objects.

