Locking a Record From Editing

An interesting conversation came up on Twitter today with @SFDCMatt. he was looking for a way to lock a record from being edited even by the record owner after some criteria was met. For a while, there were lots of ideas thrown about such as validation rules and changing the record type so it uses a different page layout. Both of these solutions weren’t ideal. Validation rules happen after the user has entered in a bunch of data and then, wham! they can’t save. So frustrating! Also, you’d have to check every field to see if it had changed. Changing the record type is another option, but users can still edit the record through the API or list view edit functionality.

We then came around to Approval Processes and how they can lock the record. Now wouldn’t it be nice if we could lock the record too? Unfortunately, there isn’t a way in workflow rules or apex to just set an isLocked field to true. Wouldn’t that be nice? Go vote now!

Now that we’ve whined about this lack of functionality, let’s try to work around it. I found that you can submit a record for approval from Apex. I immediately started thinking of triggers, so when I got home, I whipped up a solution pretty quickly. (Yeah, I lead an exciting life.)

First, let’s set up an approval process. I want my Account records to lock after the rating is changed to Cold, so I’ll set that as my entry criteria. You need at least one step in an approval, but I didn’t want to deal with it, so I set the criteria on the set to False. This means that as soon as the record is submitted for approval, it is approved and left locked!

Approval Process to Lock Record

Now all I need is a trigger to catch the event and submit the approval:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
trigger AccountRecordLock on Account (after update, after insert) {
  for (Account a : Trigger.New) {
    if (a.Rating == 'Cold') {
      // Create an approval request for the account
      Approval.ProcessSubmitRequest req1 =
      new Approval.ProcessSubmitRequest();
      req1.setComments('Automatic record lock.');
      req1.setObjectId(a.id);
 
      // Submit the approval request for the account
      Approval.ProcessResult result = Approval.process(req1);
    }
  }
}

Now as soon as I save a record with a Rating of Cold, the approval process kicks off and locks the record. Only users with Modify All permission on the object can now edit it!

You can add the Approval Process related list to the view to see that the approval was kicked off.

11 thoughts on “Locking a Record From Editing”

  1. ah.. what a neat workaround. Hope Salesforce does not see it, otherwise we may not have locking of record via apex.

    I will just add one more thing to approval process: Do NOT have more than one step in your approval process if you like it auto approve/reject. Auto approve/reject works if and only if there is one step.

      1. This was nice work but in case of masterdetail relation if status field is approved
        in parent object i want to lock the child record in child object how ca we do it?
        Plse Replay….

        Regards,
        sarvesh001

  2. Nice Article.

    1 Question why do we need to write a trigger if Approval process is supposed to be launched when the entry criteria is met?

    1. Approval processes can only be initiated with the button on the record or a trigger. The criteria you define in an approval process is only used when you submit a record for approval. In this case since we want it to automatically lock the record, we need the trigger to automatically submit the approval process for us.

  3. This is very good code snippet.. Thanks for sharing…

    BUT is there anyway through configuration (without writing the APEX code), can the record be submitted for approval automatically without user requiring to submit the Approval button?

    Salesforce should give an option to enable the auto submit for approval (like workflows gets triggered automatically)…

    Any thoughts or expert opinion on the same?

Leave a Reply