I wanted to lock and unlock records based on a check box. I know that you can do this stuff via a validation rule but I think that is difficult to maintain if adding fields. I wrote an apex trigger using Approval.Lock. I am able to Lock the record however I am unable to grammatically unlock the records. Once the record is locked if I uncheck my ISLock before update it treats it as True. IF I use After Update the record is read only even though I am making it unlocked. Below is my code.

 Trigger AccountLockRecord on Account (before insert, after update) {

     List<Account> lstUnLockAccount = new List<Account>();
     List<Account> lstLockAccount = new List<Account>();

For (Account soAccount: Trigger.New){

    If (soAccount.ISLock__c = True){
        system.debug(logginglevel.debug, 'DEBUG: lOCK' );

       // Approval.LockResult[] lrList = Approval.lock(lstAccount, false);

    } else If (soAccount.ISLock__c = False){

        system.debug(logginglevel.debug, 'DEBUG: uNlOCK' );

      //  Approval.UnlockResult[] lrList1 = Approval.unlock(lstAccount);


        Approval.LockResult[] lrList1 = Approval.lock(lstLockAccount, false);
        Approval.LockResult[] lrList2 = Approval.lock(lstUnLockAccount, false);

  • 1
    lock & unlock are treated as DMLs so you can't do these inside a for loop
    – Jarvis
    Aug 21, 2018 at 13:35
  • Ok, fine. I did it outside of the for loop and that still gives same issue. I edited the code above
    – Brooke
    Aug 21, 2018 at 13:44

1 Answer 1


== is for equality checks, = is for assignment. As such:

If (soAccount.ISLock__c = True){

will assign the value of true to IsLock__c, and this statement will always be true. For this reason, I always recommend that you avoid comparing to true/false values, because it can prevent typos like this from compiling.

If (soAccount.IsLock__c){

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .