Ever run into this frustrating little gem in your Salesforce deployment or Apex tests?
System.NoAccessException: Apex approval lock/unlock API preference not enabled
Yeah, us too. If you’re trying to use Approval.lock(record)
or Approval.unlock(record)
in Apex, Salesforce needs you to explicitly enable the feature first. Here’s how to do it — with a side of clarity and speed.
Why This Happens
Salesforce, in its infinite wisdom, has made the ability to lock or unlock records via Apex an opt-in setting. That means the feature is off by default in every org — even scratch orgs and sandboxes — unless you explicitly enable it.
So when your code hits:
Approval.lock(myRecord);
…you get slapped with a NoAccessException
unless you’ve flipped the correct switch.
How to Fix It
Here’s the step-by-step path to enabling record locking/unlocking in Apex.
If You’re Using Lightning Experience:
- Navigate to Setup
- Search for and go to:
Process Automation Settings
- Check the box:
Enable record locking and unlocking in Apex - Click Save
If You’re Using Salesforce Classic:
- Go to Setup
- Navigate to:
Create → Workflows and Approvals → Process Automation Settings
- Again, check the box:
Enable record locking and unlocking in Apex - Save and you’re golden.
🔒 Pro Tip: This setting is org-wide. Once it’s enabled, any Apex code using
Approval.lock()
orApproval.unlock()
will work as expected across your org.
Final Thoughts
This is one of those “set it and forget it” settings in Salesforce that can trip up even experienced devs. Now you know how to fix it quickly—and keep your deployment moving forward.