In Salesforce, sometimes you would want to lock records in Salesforce so that they can no longer be edited or deleted. When a record has reached the end of its lifecycle, nobody should be allowed to modify the record any longer.
For example, once an opportunity record is closed (closed won or closed lost), it just needs to live in the system without being touched. Same can be for “Cases” or records sitting in your custom objects. Or maybe once a record has been interfaced to a backend system, you would want to lock the record so that it can no longer be modified in Salesforce.
How Do You Lock Records in Salesforce?
Traditionally Salesforce professionals have used different options to meet this requirement & each of these options have its own pros and cons. They include:
- Using Record Type & Page Layouts: Create two different record types say “Open” & “Locked” and change the record type of the record using workflow field update or trigger. And then create a separate page layout for the “Locked” record type with all fields marked as ‘Read-Only’. Remove the ‘Edit’ and ‘Delete’ buttons from the page layout. However, this is not an option if you are using record types for some other purpose and do not want to change it just to meet this requirement. Also, note that using this to lock records is not a fool-proof solution as users will still be able to edit the record from the list view.
- Using Record Ownership: Automatically change the owner of the record using record-triggered flow or apex trigger and make sure that the OWD (Organization Wide Default) and sharing rules are setup correctly to prevent updates or deletes. Again, this may not be an option for you if you can’t change the record owners as it may have other impacts (e.g. reporting).
- Trigger: Write a trigger (before update, before delete) on the object and based on the desired condition raise an error. Triggers have two issues. One it is a custom development and second, the user will receive an error after they have made the changes and trying to save the record. Not a good option from the user’s experience point of view.
- Validation Rule: Write validation rule using ISCHANGED function. Here you will need to know for what all columns will you need to use the ISCHANGED function, and users will receive error once they have made the changes and trying to save record.
- Custom VF Page: Create custom Visualforce page and dynamically show/hide the ‘Edit’, ‘Delete’ buttons.
In this guide, we will explore a much better option, where we will actually “lock” the record using one of Salesforce’s out of the box declarative feature. Once locked the end users will see an error when they try to edit the record. Let’s take a look.