Apex Managed Sharing Recalculation

1. Introduction

In This blog, I am going to explain how to use apex managed sharing recalculation. You can use apex managed sharing recalculation to update the custom object sharing based on your own business logic whenever custom object OWD is changed. 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

 2.Batch Class to update sharing recalculation

You can also run sharing update programmatically using the Database.execute batch 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. Now let’s see the batch class code to update the Sharing recalculation. the final code looks as shown below.

 


3: Invoking the batch

You can run the above batch class in different ways like you can schedule the class or run it as the one-time batch.Another way is to go to Custom object –> Apex Sharing Recalculation –> Click New -> Update the above batch class as shown below.

4.Limitations

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
The class you choose must implement the Database.Batchable interface. You cannot associate the same Apex class multiple times with the same custom object.