How to Lock Records in Salesforce?

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.

Error Message when a user is trying to edit a locked record 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:

  1. 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.
  2. 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).
  3. 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.
  4. 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.
  5. 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.

Not an “All Access” Pass Member Yet?

Get Download Access to this & 150+ More Step-by-Step Guides with “All Access” Pass. A simple and single plan to access our entire library of courses, guides, workshops & masterclasses on Salesforce.

33 thoughts on “How to Lock Records in Salesforce?”

  1. I’m not able to download this User Guide. Any help would be appreciated. When I click on the Download PDF, I get an error message: This post does not exist, or may have been removed.

    1. Hi Brandon, My sincere apolgoies for the incovenience. We have corrected the link and it should work fine now. Please and let us know if you are still facing any issue. Regards, Ashish

    1. Hi Jeff, you can’t stop users having modify all permission from changing “locked” records. You will need to explore other options listed in the blog post (e.g. validation rules) to stop users from changing records.

  2. Amazing easy to follow instructions – actually the best one so far. I already subscribe to your 1 tip a day and love it!

  3. I have requirement to create vf login page for successsful login if wrong login attempt more then 3 lock the account and after 15 min unlock the account for user

    1. Hi Gagan, Locking the account after 3 unsuccessful login attempts and unlocking after 15 minutes is standard out of the box feature that you can set at Setup -> Password Policies. What is the purpose of VF Page here? As far as possible, I will recommend avoiding custom coding. You may want to consider using login flow instead.

  4. How about a validation rule on last modified date?

    A val rule like this might work instead of checking every column on the opportunity:

    ISCHANGED(lastmodifieddate) && (ISPICKVAL(StageName,”Closed Won”) || ISPICKVAL(StageName,”Closed Lost”) )

    1. Ashish Agarwal

      Hi Alekhya, ISCHANGED on LastModifiedDate does not work unfortunately. Even if you specify this validation, it doesn’t trigger and show any error message.

    2. Can we restrict users from deleting a closed won opp using a validation rule? if we do not want to go with other ways like triggers etc.

      1. Hi Kiran, Validation Rules fire only on create/update of the record. They do not fire when deleting the record so you will not be able to prevent deletion through validation rule. If you do not want to use triggers, then I think locking record will be your best option as this will prevent the deletion of record as well.

    3. Pushpendra Jain

      bu this validation rule its not locked properly when you crate a new opportunity and its stage not closed won or lost then is will edit easily
      so i want locked on all stage .

    1. Hi Ram, unlocking the record can follow the same process. For example, you can create a custom field say “Unlock Record” of checkbox type. Only specific users (like business admin or system admin) should be allowed to edit this field. Then trigger a process builder process to send this record for approval when the value of the field is checked. And the approval process will unlock the record.

  5. We’re a very small company were the Admin also has business tasks. We want to prevent even the Admin of editing records once they achieve a certain status.
    However, we want the have the option for the Admin to Edit something if necessary. Any ideas how to achieve this?
    We’ve thought of a TRUE / FALSE field that only the Admin can see and edit to Lock / Unlock for the Admin?
    Thanks

    1. Hi Pedro, Admins can modify any record in Salesforce – even the locked records. You can use Field History Tracking to track the changes made the admin but that is more of a reactive approach. You can use a TRUE/FALSE field with a validation rule to allow or disallow changes based on the value of the TRUE/FALSE field but do note that admins can always disable the validation rule, make changes and enable it again.

  6. Your option “Using Record Type & Page Layouts” is missing the disadvantage of mass editor updates. When “Using Record Type & Page Layouts” to lock a record, it still allows you to edit fields using Mass Editor in a list view. How do you really lock down the entire record? In my research, “Using Record Type & Page Layouts” gives users a lock when they view the record but no locks when they try to edit using Mass Editor. Please explain this use case. Thank you.

    1. Your observation is correct MK. Record types with page layouts will still allow you to edit the records in the list view. This is not a fool-proof solution for locking records. The best way to lock records will be through the approval process that I have mentioned in this blog. I have updated the blog to include the point highlighted by you, so thanks for your feedback.

  7. Great work around. Since been few releases since than. Are you aware of any way of relocking record. We had Opportunities unlocked by someone and no way to relock without going thru approval process again that I can find.

  8. Error:
    Update failed. First exception on row 0 with id 00Q22000001F4nLEAS; first error: CANNOT_EXECUTE_FLOW_TRIGGER, The record couldn’t be saved because it failed to trigger a flow. A flow trigger failed to execute the flow with version ID 301220000000ZNe. Flow error messages: An unhandled fault has occurred in this flowAn unhandled fault has occurred while processing the flow. Please contact your system administrator for more information. Contact your administrator for help.: []

    actual process is when i am trying convert a lead in custom convert.after convert lead ststus set as a Custom Converted. after that i need to set record lock. so i am trying to set record lock by using the process bulider and Approval process.in that process i got this issuse.please fix this issuse.

  9. i got this issuse.
    Error:
    Update failed. First exception on row 0 with id 00Q22000001F4nLEAS; first error: CANNOT_EXECUTE_FLOW_TRIGGER, The record couldn’t be saved because it failed to trigger a flow. A flow trigger failed to execute the flow with version ID 301220000000ZNe. Flow error messages: An unhandled fault has occurred in this flowAn unhandled fault has occurred while processing the flow. Please contact your system administrator for more information. Contact your administrator for help.: []

    actual process is when i am trying convert a lead in custom convert.after convert lead ststus set as a Custom Converted. after that i need to set record lock. so i am trying to set record lock by using the process bulider and Approval process.in that process i got this issuse.please fix this issuse.

  10. Thanks for the post.. very neat!
    Can you post an example for unlocking a record as well? say on some other status value.. Thanks again.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top
Introducing All Access Pass