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:

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.');

      // 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.


2 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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s