Apex Managed Sharing Recalculating

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

Step 1: Create Batch Class to update sharing recalculation
You can also run sharing update 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.
Now let’s see the batch class code to update the Sharing recalculation. the final code looks as shown below.

global class DeliverySharingRecalculation implements Database.Batchable<sObject> {

global Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator([SELECT Id, Name FROM Delivery__c]);
}

global void execute(Database.BatchableContext BC, List<sObject> scope){
// In this case I am just simply sharing the data with one user. But you
//can adjust the data based on different conditions. Examples If Delivery mode is express you can assign to specific group id Delivery
// is normal you can assign to some other group
List<Delivery__Share> newDelivary = new List<Delivery__Share>();
List<Delivery__c> deli =(List<Delivery__c>)scope ;

for(Delivery__c de : deli){
Delivery__Share delSH = new Delivery__Share();
// Assign it to user or group
delSH.UserOrGroupId = '00541000000RlIa';
//Access level to read
delSH.AccessLevel = 'Read';
// Record Id
delSH.ParentId = de.Id;
// Row Cause
delSH.RowCause = Schema.Delivery__Share.RowCause.Custom_Sharing_Model__c;
newDelivary.add(delSH);
// You can add new sharing and delete the existing sharing based on requirementts
}
Database.SaveResult[] lsr = Database.insert(newDelivary,false);

}

global void finish(Database.BatchableContext BC){
}

}

Step 2: 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.


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.