[SalesForce] Why can’t I lock/unlock records through apex

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?

Best Answer

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

Related Topic