Tuesday 25 July 2017

How to Lock/Unlock record thorough Apex code ?

Hi All,

We all have faced many business scenario where we have to lock/unlock records. We had traditional approach to create separate record type and page layout to achieve it, but salesforce has given API to do thorough apex code.

Please make sure below settings is enabled.

Locking Record 

1. Create a standard Approval process which will take care of locking records.

http://salesforceworld4u.blogspot.com/2017/07/how-to-supress-standard-approval-email.html

2. Invoke Approval process
  a. Using Process builder (Submit for Approval)
  b. Use Apex code to invok from trigger or custom button.

 /*
    * This method will invoke the standard approval process
    */
    public static void submitForApproval(Case caseRecord)
    {
        // Create an approval request for the Opportunity
        System.debug('Printing insyde approval process method');
        Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
        req1.setComments('Submitting request for approval automatically using Button');
        req1.setObjectId(caseRecord.id);
        //req1.setNextApproverIds(new Id[] {case.owner});
        

        // Submit the approval request for the Opportunity
        Approval.ProcessResult result = Approval.process(req1);

    }

If there is no approval process defined still we can lock the record.


// Query the accounts to lock
Case [] cases = [SELECT Id from Case];
// Lock the accounts
Approval.LockResult[] lrList = Approval.lock(cases, 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('Case fields that affected this error: ' + err.getFields());
        }
    }
}

Unlocking record

Below is the code which will unlock the record.

 /*
    * This method will unlock case record.
    */
    public static void unlockCaseRecord(Case caseRecord) {
        Approval.UnlockResult unlockedRersult = Approval.unlock(caseRecord);
        // Iterate through each returned result
        if (unlockedRersult.isSuccess()) {
            // Operation was successful, so get the ID of the record that was processed
            System.debug('Successfully unlocked opportunity with ID: ' + unlockedRersult.getId());
        }
        else {
            // Operation failed, so get all errors                
            for(Database.Error err : unlockedRersult.getErrors()) {
                System.debug('The following error has occurred.');                    
                System.debug(err.getStatusCode() + ': ' + err.getMessage());
                System.debug('opportunity fields that affected this error: ' + err.getFields());
            }
        }
       
        
    }   

No comments: