Lock/Unlock Record Using Apex In Salesforce

Author: Rishabh Dubey

Whenever we create an approval process we get an option to Lock/Unlock records in the initial submission action, final approval, and rejection action.

We can also perform the same actions by using apex lock() and unlock() methods in the system approval namespace by passing in record IDs or sObjects.

Step 1: Enable record locking and unlocking in Apex
  1. Go to setup
  2. Search for Process Automation setting
  3. Check the checkbox for Enable record locking and unlocking in Apex
  4. Click Save
Step 2: Now you can use apex lock() and unlock()  in your custom codes.


Requirement: Suppose you want to Lock the Account record as soon as the status is changed to Inactive.

This can be done in various ways. I have created an Invocable action and flow for the same.

a. Created below Class with Invocable Action

public class LockRecord {

@InvocableMethod (label='Lock Accounts')

public Static void LockAccounts(List<Id> listAccIds){

List<Account> listAccountsToLock= [Select Id, Name from Account where id =:listAccIds]  ;

  for (Account accountToLock : listAccountsToLock)






b. Created a record trigger flow to invoke this Action

Configured flow as below:

Added the Lock Account Action.

Saved and Activated the flow.

c. To test the automation I changed the account status to Inactive on a test record and the record got locked successfully. Now only the users with “Modify all data” or “Modify All on Account level” can edit this record.

We use cookies on this site to enhance your user experience. For a complete overview of how we use cookies, please see our privacy policy.