Sub topic 9
How record locking works during approval
This chapter assumes you can configure a basic approval process, know the difference between users, profiles, and permission sets, and can read short Apex snippets. It covers what a lock is, when it is applied and removed, who can still edit a locked record, and how to inspect or change locks from Apex.
What a lock is
When a record is submitted for approval, Salesforce locks it. A locked record is read-only for most users. They can still view it and see its approval history, but they cannot save changes through the UI, the API, or Apex DML.
The lock is attached to the record while it has a pending approval request. It is not a field or a sharing rule. You cannot see it in a field list, and it does not change who can read the record.
The lock exists to protect the data being approved. If a sales rep could change the discount after the manager has read it, the approval would mean nothing.
The lock lifecycle
The process definition and the outcome of the request decide the lock state.
The table below summarizes each event.
| Event | Lock result | Notes |
|---|---|---|
| Initial submission | Locked | Applied automatically. The record is locked from the moment the request is created. |
| Intermediate step approved | Stays locked | The record moves to the next step. |
| Final approval | Locked by default | Choose "Unlock the record" in the final approval actions to release it. |
| Final rejection | Unlocked | The submitter can edit and resubmit. |
| Recall | Unlocked | Recall removes the pending request. |
The main point to remember is that final approval keeps the lock unless you change it. Many teams want an approved quote or contract to stay frozen. Others want the record editable again so work can continue. Make this an explicit design decision for each process.
Who can edit a locked record
Three groups can edit a locked record.
- Administrators, meaning users with the Modify All Data permission. They can always edit locked records.
- The currently assigned approver, but only if you allow it on the process.
- Anyone, after the record is unlocked.
The second option is controlled by the Record Editability Properties setting on the approval process. It has two choices:
- Administrators only can edit records during the approval process.
- Administrators or the currently assigned approver can edit records during the approval process.
The second choice is useful when approvers fix small data errors, such as a mistyped amount. Editing is still limited by the approver's field-level security and page layout. The setting does not give an approver edit access to fields they could not edit otherwise.
An approver's edit rights apply only while they are the currently assigned approver. When the request moves to the next step, a different person holds the work item, and the earlier approver loses edit access.
What a lock blocks and what it does not
A lock blocks edits to the locked record itself. Several things still work, and they often surprise people.
- Field updates defined as approval actions run on locked records. This is how a process can set a Status field to "Approved" while the record is locked. Those updates are part of the process, so the lock does not stop them.
- Approving, rejecting, and reassigning work items are not edits to the record. They work normally.
- Reading, reporting, and viewing the approval history are unaffected.
- Record locks apply to Apex. Apex code usually runs in system mode, but it does not bypass an approval lock. A trigger, batch job, or flow that updates a locked record as a normal user fails.
That last point causes many production errors. A nightly batch job updates opportunities and hits a few that are mid-approval. Those rows fail with an error like "This record is locked. If you need to edit it, contact your admin."
Handling lock errors in Apex
The failing status code is ENTITY_IS_LOCKED. You can catch it and decide what to do with the record instead of letting the whole transaction fail.
public with sharing class OpportunityUpdater {
public static void applyDiscount(Id opportunityId, Decimal discount) {
Opportunity opp = new Opportunity(Id = opportunityId, Discount__c = discount);
try {
update opp;
} catch (DmlException e) {
if (e.getDmlType(0) == StatusCode.ENTITY_IS_LOCKED) {
System.debug('Skipped locked record ' + opportunityId);
} else {
throw e;
}
}
}
}For bulk operations, Database.update(records, false) is a better fit. It allows partial success, and you can read Database.SaveResult.getErrors() for each failed row. Checking err.getStatusCode() == StatusCode.ENTITY_IS_LOCKED lets you collect the locked records and retry them later.
Checking and changing locks from Apex
The Approval class has methods for lock state. Approval.isLocked returns true if a record is locked. Approval.lock and Approval.unlock change it.
public with sharing class ApprovalLockService {
public static Boolean unlockIfLocked(Id recordId) {
if (!Approval.isLocked(recordId)) {
return true;
}
Approval.UnlockResult result = Approval.unlock(recordId);
if (!result.isSuccess()) {
for (Database.Error err : result.getErrors()) {
System.debug(LoggingLevel.ERROR,
err.getStatusCode() + ': ' + err.getMessage());
}
}
return result.isSuccess();
}
}Both lock and unlock also accept a list of IDs, and an allOrNone flag when you want partial results. The result objects expose isSuccess(), getId(), and getErrors().
Unlocking in the UI
Administrators have several ways to release a lock.
- Open the Approval History related list on the record and use Recall. This ends the pending request and unlocks the record. Admins can do this even when they are not the submitter.
- Edit the record directly. Admins can save changes on a locked record without unlocking it first.
- Configure the final approval action to unlock the record, so no one has to do it by hand.
A user who is not an administrator can unlock a record only by recalling their own pending request, if the process allows recall. Whether recall is available depends on the process setting "Allow submitters to recall approval requests".
Design patterns and common problems
Use these patterns when the lock gets in the way.
- A record must stay editable during approval. An approval process is the wrong tool. Approvals lock by design. Consider a custom status field with a flow, or split the data that needs approval into a separate record.
- An approver needs to correct data. Enable "Administrators or the currently assigned approver can edit records". Also check that the approver's page layout shows the field as editable.
- A related child record fails to save. Child records are separate records, so the parent's lock does not block them directly. Failures usually come from a roll-up summary field or a trigger on the child that updates the locked parent. Look for code that writes back to the parent.
- A batch job fails on a few rows. Query for records with a pending approval and skip them, or use partial-success DML and reprocess the rejected rows later. Locked rows can be found with this query:
List<ProcessInstance> pending = [
SELECT TargetObjectId
FROM ProcessInstance
WHERE Status = 'Pending'
];- A record stays locked after approval, and users complain. This is the default behavior. Add an unlock action to the final approval actions.
- A test cannot update a record after submission. The record is locked in tests too. Either call
Approval.unlockfirst, or run the update as a user with Modify All Data.
Exercise
A process has two steps. Step 1 is assigned to a manager, and step 2 to a director. The process allows the currently assigned approver to edit records. The manager approves step 1, and then tries to fix a typo in the record. What happens, and what lock state does the record have after the director gives final approval if you did not add any unlock action?
Show answer
The manager's edit fails with ENTITYISLOCKED. The record is still locked, and the manager is no longer the currently assigned approver, because the work item now belongs to the director. After the director's final approval, the record stays locked. Final approval keeps the lock by default, so the process needs an explicit "Unlock the record" action in its final approval actions to release it.
Exercise
A nightly Apex batch updates 200 opportunities with Database.update(records, false). Twelve rows fail with ENTITYISLOCKED. Name one way to find out which rows failed, and one design change that avoids the failures.
Show answer
Loop over the Database.SaveResult list and, for each result where isSuccess() is false, read getErrors() and compare getStatusCode() to StatusCode.ENTITYISLOCKED to collect those record IDs. To avoid the failures, exclude records with a pending ProcessInstance from the batch query, or collect the locked IDs and retry them in a later run after the approvals complete.