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
Srinivas223Srinivas223 

Unlock record from apex

I am trying to unlock the records locked from approval process using apex. I am trying to do it in the After Update context. Can I update same record in after update context?
Here is my sample code
public static void unlockApprovalLockedRecords(list<customObject> records){
       
        for(customObject sp : records){
        
       if(Approval.isLocked(sp.id))
     
     Approval.unlock(sp.id);
}

I am using handler classes and calling this method from the handler class.
Thank you.
Om PrakashOm Prakash
Yes you can unlock the record from after update context.
If you are getting System.NoAccessException then you need to follow bellow steps in your org:
Go to
Setup > Create > Workflows and Approvals > Process Automation Settings
And mark the checkbox  Enable record locking and unlocking in Apex as true and save.
User-added image
Pradeep SinghPradeep Singh
Hi,
To update in after Update, you have to query the record/s from the database and then perform action on that record/s and do Update DML on these.