[SalesForce] unlock record submitted for Approval

When a record is submitted for approval, it is locked by default. But as per the recent change by Salesforce, it can be unlocked using Apex class. I have this below Apex class which unlocks the record submitted for approval on Account.

public class Unlock_Approval_Account {
   public static void unlockAccountRecord(Account AccountRecord) { 
        Approval.UnlockResult unlockedRersult = Approval.unlock(AccountRecord);
        // 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 Account 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('Account fields that affected this error: ' + err.getFields());
            }
        }
    }  
    }

This code got saved without any compilation errors. But when I have my record submitted for approval, it isn't unlocking the record. I have also enabled "Lock or Unlock Record using Approval Process" in the Process Automation settings
Can anyone help me with this.

Best Answer

S kanth, did you cheked the "Enable record locking and unlocking in Apex as true" on in settings? if not do that (in case). the logic looks perfect to me.

updating my answer from here, can try to make one method with void type , and pass the account list , instead of making static it.

Related Topic