0

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.

  • It should work properly, I tried the same code in my dev org. Please try adding without sharing to the class. This may be the issue related to access. – rahul gawale Mar 6 at 12:14
  • @rahulgawale, I tried adding without sharing to the class but that didn't work either. Is there anything that I'm missing out? The approver is not admin and have some restricted access. Is this stopping the record from being unlocked? – S kanth Mar 6 at 12:20
  • 1
    Can you please check the debug logs and confirm if it is going inside the success condition ? If not, then what is the error that you're getting ? – Vijay Ganji Mar 6 at 14:55
1

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.

  • Yes I did....I have mentioned that in my question as well – S kanth Mar 6 at 11:54
  • i have updated answer try that once. if not let me know i have another one. – Umesh Beti Mar 6 at 12:03
  • Thanks for your reply. I tried removing static and passing account list but that too didn't work...is there any other alternative? – S kanth Mar 6 at 12:06
  • can you update your answer once , let me check with your code. – Umesh Beti Mar 6 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.