1

In my attempts to use the Approval class methods to unlock locked records or to lock unlocked records, I have encountered the following warning:

System.UnexpectedException: null

I saw this error both when running anonymous apex:

Approval.lock('0019000001dBwi4');

and when using a trigger:

trigger AccountTrigger on Account (after insert, after update) {
    Approval.lock(trigger.new,false);
}

Does anyone know what would be causing this error or how I could get around it?

4
  • Did you enable locking/unlocking with apex? releasenotes.docs.salesforce.com/en-us/winter16/release-notes/….
    – kurunve
    Jan 14, 2016 at 8:29
  • I have strong feeling that salesforce throws different exceptions in the same issues due to locale. When that settings is disabled, it throws System.UnexpectedException as well, but with more information
    – kurunve
    Jan 14, 2016 at 8:30
  • @kurunve I have that enabled. Disabling that checkbox gives me a different error: System.UnexpectedException: Apex approval lock/unlock api preference not enabled.
    – martin
    Jan 14, 2016 at 8:32
  • If you enable and still see an error while lock/unlock check the file api version - this fixed my issue. Oct 31, 2017 at 11:47

3 Answers 3

5

I think you need to lock the record after insert or update. Not before. Try changing that to after event.

It does not make sense to lock a record before you have inserted it.

EDIT

Tested and works fine on API version 35

6
  • Agreed. I'm still seeing the error when using an after trigger though.
    – martin
    Jan 14, 2016 at 9:02
  • Also, that wouldn't explain the issue with anonymous APEX using an explicit Id.
    – martin
    Jan 14, 2016 at 9:03
  • I tested on after insert on account and it worked fine in the trigger. And just now tested from the anonymous code and saw no problems.
    – AslamK
    Jan 14, 2016 at 9:06
  • 1
    which instance is your org on @martin ?
    – AslamK
    Jan 14, 2016 at 9:15
  • It looks like that was the problem. The debug log header was showing my anonymous apex was running the 33.0 api, and I hadn't updated the trigger version either. I am a bit surprised that it would compile and give me a run-time error though.
    – martin
    Jan 14, 2016 at 9:20
0

There appears to be some issue when using Apex to lock records in Salesforce if your sandbox is on Spring '16 (metadata version 36). To work around the issue the metadata version of Apex classes locking records should be set to 36. This may make production deployments an issue until the issue has been fixed / all production environments have been upgraded to Spring '16.

-2

have a custom field updated from the approval process and let it fire the trigger

*In the trigger check for the condition of the updated field and call a future method with the unlock record code.

*this ensures two transactions and it works!

public with sharing class UnlockRecord {   
    @future public static void Unlock(List<Id> requests) {
        for(Id accountId :requests){
            try{
                Approval.UnlockResult unlockResult = Approval.unlock(accountId);
            }
            catch(exception ex){                 
                system.debug('Exception*****'+ex);
            }
        }
    }
}
2
  • The accepted answer on this question and its comments suggest that a new transaction is not required. Do you have different information to share?
    – David Reed
    Dec 20, 2021 at 23:44
  • 2
    You should never swallow exceptions as shown above.
    – David Reed
    Dec 20, 2021 at 23:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .