Any idea how to "Enable record locking and unlocking in Apex" for scratch org?

I am getting the following error while running unit tests in scratch org:

System.NoAccessException: Apex approval lock/unlock api preference not enabled.

Appreciate your insight on this or if you know any workaround.

  • as a workaround, you can manually enable that -- salesforce.stackexchange.com/questions/108524/…
    – kurunve
    Dec 18 '18 at 16:51
  • We've already considered it but that's not an ideal alternative, as we would like to spin up scratch orgs for CI/CD purposes. Appreciate your input though.
    – Tanmay
    Dec 18 '18 at 16:54

As of Winter '19, you can specify the setting ApexApprovalLockUnlock from the Metadata API's OrgPreferenceSettings entity as part of your scratch org configuration file. (Deploying settings entities with the Metadata API continues to work).

Under the "settings" key, you nest your settings entities and translate their names into camelCase:

In Winter ’19 and later, scratch org settings are the format for defining org preferences in the scratch org definition. Because you can use all Metadata API settings, they are the most comprehensive way to configure a scratch org. If a setting is supported in Metadata API, it’s supported in scratch orgs.


Although the Settings are upper camel case in the Metadata API Developer Guide, be sure to indicate them as lower camel case in the scratch org definition.

When converting existing org prefs to settings, the org prefs that start with “Is” have a corresponding setting that starts with “enable”. For example, “IsOrdersEnabled” is “enableOrders” that takes a Boolean value of true or false.

Here, you can do this in your scratch org config file:

  "orgName": "Acme",
  "edition": "Enterprise",
  "settings": {
        "orgPreferenceSettings": {
            "apexApprovalLockUnlock": true

Then your scratch orgs will spin up with the setting already enabled:

enter image description here

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.