How to Lock Records in Salesforce?
In Salesforce, sometimes you 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. The same can be for "Cases" or records in your custom objects. Or maybe once a record has been interfaced to a back-end 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, and each of these options has its own pros and cons. They include:
Using Record Type and Page Layouts: Create two different record types, say, "Open" and "Locked," and change the record type of the record using a workflow field update or a trigger. 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 foolproof 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 a record-triggered flow or an Apex trigger and make sure that the OWD (Organization-Wide Default) and sharing rules are set up 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 custom development, and second, the user will receive an error after they have made the changes and is trying to save the record. Not a good option from the user experience point of view.
Validation Rule: Write a validation rule using the ISCHANGED function. Here, you will need to know for which columns you will need to use the ISCHANGED function, and users will receive an error once they have made the changes and are trying to save the record.
Custom VF Page: Create a custom Visualforce page and dynamically show/hide the Edit and 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 features. Once locked, the end users will see an error when they try to edit the record. Let's take a look.