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?

  • Did you enable locking/unlocking with apex? releasenotes.docs.salesforce.com/en-us/winter16/release-notes/…. – kurunve Jan 14 '16 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 '16 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 '16 at 8:32
  • If you enable and still see an error while lock/unlock check the file api version - this fixed my issue. – user1786607 Oct 31 '17 at 11:47
4

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

  • Agreed. I'm still seeing the error when using an after trigger though. – martin Jan 14 '16 at 9:02
  • Also, that wouldn't explain the issue with anonymous APEX using an explicit Id. – martin Jan 14 '16 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 '16 at 9:06
  • 1
    which instance is your org on @martin ? – AslamK Jan 14 '16 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 '16 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.

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.