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
Day dreamerDay dreamer 

I used Approval.unlock() methods, but it didnt unlock my record.

 Inside my trigger method i wrote below code:

public void OnBeforeUpdate(List<Account> newAccList ,List<Account> oldAccList ){
        Approval.UnlockResult[] lrList = Approval.unlock(oldAccList, false);
        system.debug('-----lrList-----'+lrList);
}
In debug log below ,I have no errors and isSucess flag set to true, but my record is still locked :
----lrList-----(Approval.UnlockResult[getErrors=();getId=00163000004DNG6;isSuccess=true;])

Has anyone used this new feature of winter 16 sucessfully?or can anyone advice why it is not working for me?
 
Sudipta DebSudipta Deb
Hi,

See the below documentation.
https://releasenotes.docs.salesforce.com/en-us/winter16/release-notes/rn_apex_approval_locks_unlocks.htm

Instead of doing System.Debug with the result, do the below thing -
// Iterate through each returned result
for(Approval.UnlockResult lr : lrList) {
    if (lr.isSuccess()) {
        // Operation was successful, so get the ID of the record that was processed
        System.debug('Successfully unlocked account with ID: ' + lr.getId());
    }
    else {
        // Operation failed, so get all errors                
        for(Database.Error err : lr.getErrors()) {
            System.debug('The following error has occurred.');                    
            System.debug(err.getStatusCode() + ': ' + err.getMessage());
            System.debug('Account fields that affected this error: ' + err.getFields());
        }
    }
}

The record isn't unlocked until the record is approved. Just because it went from an insert to an update does not mean it is now approved. In fact, Approval Processes aren't processed until the end of the transaction, so it's considered locked until the end of the transaction, when it is updated via the workflow rule processing step. Furthermore, records that enter the approval process will stay locked until they are actually approved or removed from the process.

Check the Order of Exceution also -
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_order_of_execution.htm

Please select this as "best answer" if it helps. Thanks.