function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Anonymous DeveloperAnonymous Developer 

Need Help creating apex action to unlock record

Here's my code

public class AS_UnlockApproval {

    @InvocableMethod (label = 'Unlock Record Brokerage Request' description = 'Unlock Brokerage Request Object' category = 'Brokerage Request')
    public static List<Response> Unlock(List<Request> requests) {
        List<Response> responseWrapper= new List<Response>();
        
        for (Request i:  requests) { 
            String recordId = i.recordId;
            
            Approval.UnlockResult unlockResult = Approval.unlock(recordId);

            Response response = new Response();
            
            response.isSuccess = unlockResult.isSuccess();
            response.errors = packErrorString(unlockResult);

            responseWrapper.add(response);
            
        }
        return responseWrapper;
    
    }

    public static String packErrorString(Approval.UnlockResult unlockResult) {
        String errorString = '';
        for(Database.Error err : unlockResult.getErrors()) {
            errorString = errorString + (' The following error has occurred.');                   
            errorString = errorString + (' ' + err.getStatusCode() + ': ' + err.getMessage());
        }
        return errorString;

    }


    public class InvocableErrorException extends Exception{}

    public class Request {

      @InvocableVariable
      public String recordId;
      
    }
    
    public class Response {
     
       @invocableVariable
        public Boolean isSuccess; 
        
        @invocableVariable
        public String errors;
    }
    public class InvocableActionException extends Exception {}
}

I already enabled the process automation on record locking and unlocking.

User-added image
Am I missing something?

I just need to unlock the approval process automatedly since it's not editable. Need Help. Thanks.

Anonymous DeveloperAnonymous Developer
I am using the program engagement custom object (ProgramEngagement__c) to unlock
AnkaiahAnkaiah (Salesforce Developers) 
Hi,

Refer the below link.
https://www.biswajeetsamal.com/blog/set-approval-process-lock-and-unlock-records-using-apex-code/

Thanks!!