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
Our Man In BananasOur Man In Bananas 

Error when locking a record in BeforeUpdate trigger

I need to lock a record in my trigger after a member of the systems team has changed it.
I tried this code:
// Lock the request
Approval.lock(rq.Id);
rq is the record in my custom object.

The error I get is:
Apex trigger System_Change_Request_Events caused an unexpected exception, contact your administrator: System_Change_Request_Events: execution of BeforeUpdate caused by: System.UnexpectedException: null: Trigger.System_Change_Request_Events: line 315, column 1
line 315 is the one above that tries to lock the record.
It saved the trigegr changes ok, so the method signature must be correct.

I need to lock the record as once this part of the process has been reached there should never be any need to edit it.
But it has to be unlocked after it has been approved by the Line manager in order that systems can convert the request into a demand (by changing the status which kicks off the BeforeUpdate trigger).

in the Process Automation Settings this has been enabled
So what am I missing here and what is the correct way to do this in Apex?
pconpcon
Can you provide the full trigger that you are doing this in.  It looks like you are probably calling this from a before insert portion of the trigger.  This would mean that the rq does not have an Id set yet.

NOTE: When including code please use the "Add a code sample" button (icon <>) to increase readability and make referencing code easier.