Simple - Locking Records with Triggers
- abhyash
- Nov 5, 2022
- 1 min read
Updated: Nov 6, 2022
V1 Prepared By: Abhyash Timsina – 5 November 2022
Motivation: Imagine there is a requirement where you need to lock a Case record so non-admins cannot access. Maybe you don’t want multiple users working on the same record. Here I go through how you can lock and unlock a record using apex and triggers. This is a very simple example. Note – This is a personal project and not intended for financial gain.
My Technical Components

Prerequisite
To allow record locking and unlocking - From Setup, enter Process Automation Settings in the Quick Find box, then click Process Automation Settings. Then, select Enable record locking and unlocking in Apex.
Source Code for LockUnlockCase Apex Class
/**
* Created by Abhyash Timsina on 5/11/2022.
*/
public with sharing class LockUnlockCase {
public static void LockCase(List<Case> lockCases){
Approval.LockResult[] lrList = Approval.lock(lockCases, false);
}
public static void UnlockCase(List<Case> unlockCases){
Approval.UnlockResult[] lrList = Approval.unlock(unlockCases, false);
}
}
LockUnlockCaseTrigger
trigger LockUnlockCaseTrigger on Case (after update) {
for (Case c : trigger.new) {
if (c.Lock_Case__c == false) {
LockUnlockCase.UnlockCase(trigger.new);
}
if (c.Lock_Case__c == true) {
LockUnlockCase.LockCase(trigger.new);
}
}
}
Now all you need to do is go to a Case record and tick Lock Record as Systems Administrator. When you log in as a non-admin, and try to open the same record – it will say something like – record cannot be found.
Package Link:
If you need help in installing and getting this working – please contact me on LinkedIn
Comentarios