+ Start a Discussion

Question about Approval.unlock() and unlocking records.

I was messing with unlocking opportunities.  The business requires Opportunities submitted for Approval in the "Signed and Submited for Approval" stage to be unlocked.  Currently in the UI when you do this it locks the record and places an unlock button that allows you to unlock the record only if you have an admin profile/modify all permission.  I used the sample code from the method guide to make this trigger:
trigger triggerUnlock on Opportunity (after update) {
Opportunity[] oList = [SELECT Id from Opportunity Where Id IN : Trigger.new and StageName = 'Signed and Submitted for Approval']; 

Approval.UnlockResult[] urList = Approval.unlock(oList, false);

for(Approval.UnlockResult ur : urList) {
    if (ur.isSuccess()) {
        // Operation was successful, so get the ID of the record that was processed
        System.debug('Successfully unlocked opportunity with ID: ' + ur.getId());
    else {
        // Operation failed, so get all errors                
        for(Database.Error err : ur.getErrors()) {
            System.debug('The following error has occurred.');                    
            System.debug(err.getStatusCode() + ': ' + err.getMessage());
            System.debug('Opportunity fields that affected this error: ' + err.getFields());

Using debug logs I can tell that it is unlocking the opportunity record when it is submitted for approval.  But in the UI it still has the unlock button and the record is locked.  So is the "Approval.unlock()" method used specifically to unlock the record only for apex uses?  Does record lock itself again after all code has ran?  Is there something I can do to keep the record unlocked so a user can still edit it?  Thanks
SandhyaSandhya (Salesforce Developers) 
Hi syric

From below link, I found this  I suggest you focus on these points.


To enable this feature, from Setup, enter Process Automation Settings in the Quick Find box, then click Process Automation Settings. Then, select Enable record locking and unlocking in Apex.

Salesforce admins can edit locked records. Depending on your approval process configuration settings, an assigned approver can also edit locked records. Locks and unlocks that are set programmatically use the same record editability settings as other approval-process locks and unlocks. Record locks and unlocks are treated as DML. They’re blocked before a callout, they count toward your DML limits, and if a failure occurs, they’re rolled back along with the rest of your transaction. To change this rollback behavior, use anallOrNone parameter.

Hope this helps you!

Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, you can give me kudos.
Thanks and Regards

Jenny Mosunova TelitJenny Mosunova Telit
We are facing the same problem with  this Approval.unlock function. I created a trigger which tries to unlock Account record when it is sent to approval. The thing is, it works on Admin Profile user but not on other Profiles, despite I get success in Approval.UnlockResult for both. I assume that the problem might be in permissions, anyone knows something regarding this issue?
Mukesh Kumar 107Mukesh Kumar 107
Concept: Your triggers is running/initiated through original Approval Process. You assumed that the Record is Locked in the beginning of Approval Process. This assumption is WRONG. In other words, when your trigger ran, please notice, you record was still UNLOCKED, and it did not unlock through your Apex code. Locking happens after your Apex code ran. Locking happens at the end of the transaction. You will have to find a different way to call your Apex to unlock the record. It could be scheduler, Batch, or custom Approval Process, or call Approval Process through a Process Builder.