Lock/Unlock Records Using Apex in Salesforce : jayakrishnasfdc

Lock/Unlock Records Using Apex in Salesforce
by: jayakrishnasfdc
blow post content copied from  Jayakrishna Ganjikunta
click here to view original post


Whenever we develop Approval Process in salesforce if we do not set final approval/final rejection step as unlock, the records processed in approval will stay in locked condition. To unlock those records in not possible through salesforce lightning, we have to switch to classic version individually.
But it is not suggestable way when we have many records in locked. So at this case we can run a script/below line of code in anonymous window in developer console and unlock it.

For this, We can use apex lock() and unlock() methods in the System.Approval namespace to lock and unlock records by passing in record IDs or sObjects.

But this will work only we enabled below settings in our salesforce org.

go to Setup | Search Automation Settings in the Quick Find box | click on Automation Settings. Then, select Enable record locking and unlocking in Apex.

Let’s consider an example CPQ Quote object, to unlock records.

First Query how many quotes created in a month.
SELECT Id From SBQQ__Quote__c where CreatedDate > 2021-03-01T13:27:00-07:00 AND CreatedDate < 2021-03-30T13:27:00-07:00
So, we will know the Number of Quotes created in period of time.

Next Execute Below Script to Unlock


SBQQ__Quote__c[] Quts = [SELECT Id From SBQQ__Quote__c where CreatedDate > 2021-03-01T13:27:00-07:00 AND
                                                                                                                                 CreatedDate < 2021-03-30T13:27:00-07:00];
// If you have one/many SBQQ__Quote__c[] Quts = [SELECT Id From SBQQ__Quote__c where Id IN ('','')];
Approval.UnLockResult[] lrList = Approval.Unlock(Quts, false);
// Iterate through each returned result
for(Approval.UnlockResult lr : lrList) {
    if (lr.isSuccess()) {
        System.debug('Successfully Unlocked Quote with ID: ' + lr.getId());
    }
    else {
        // Operation failed, so get all errors                
        for(Database.Error err : lr.getErrors()) {
            System.debug('The following error has occurred.');                    
            System.debug(err.getStatusCode() + ': ' + err.getMessage());
            System.debug('Quote fields that affected this error: ' + err.getFields());
        }
    }
}
    System.debug('Total Quotes Unlocked in the Execution : ' + lrList.size());

Other Example with Account Object
Unlock


//Get records to unlock
List<Account> AccList = [SELECT Id From Account LIMIT 10];
//Check locked records
List<Account> AccLockList = new List<Account>();
for(Account a :AccList){
    if(Approval.isLocked(c.id)){
        AccLockList.add(c);
    }
}
//Unlock record
if(!AccLockList.isEmpty()){
    //Unlock records
    List<Approval.UnlockResult> ulrList = Approval.unlock(AccLockList, false);
     
    // Iterate through each returned result
    for(Approval.UnlockResult  ulr : ulrList) {
        if (ulr.isSuccess()) {
            //Operation was successful, so get the ID of the record that was processed
            System.debug('Successfully locked account with ID: ' + ulr.getId());
        }
        else {
            //Operation failed, so get all errors                
            for(Database.Error err : ulr.getErrors()) {
                System.debug('The following error has occurred.');                    
                System.debug(err.getStatusCode() + ': ' + err.getMessage());
                System.debug('Account fields that affected this error: ' + err.getFields());
            }
        }
    }
} 

Lock

//Get Account records to lock
List<Account> AccList = [SELECT Id From Account LIMIT 10];
//Lock records in Above List
List<Approval.LockResult> lrList = Approval.lock(AccList, false);
 
// Iterate through each returned result
for(Approval.LockResult lr : lrList) {
    if (lr.isSuccess()) {
        //Operation was successful, so get the ID of the record that was processed
        System.debug('Successfully locked account with ID: ' + lr.getId());
    }
    else {
        //Operation failed, so get all errors                
        for(Database.Error err : lr.getErrors()) {
            System.debug('The following error has occurred.');                    
            System.debug(err.getStatusCode() + ': ' + err.getMessage());
            System.debug('Account fields that affected this error: ' + err.getFields());
        }
    }
}

Thanks for Reading..
Jayakrishna


May 02, 2021 at 03:20PM
Click here for more details...

=============================
The original post is available in Jayakrishna Ganjikunta by jayakrishnasfdc
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================

Salesforce