How to Lock Records in Salesforce?

Lock Records 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.

So, then how do you lock records in Salesforce so that it can no longer be modified?

Traditionally there has been a few options

  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: Change the owner of the record using workflow field update or trigger and make sure that the OWD (Organisation 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 once 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 again 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

I think, something like a workflow action called ‘Lock/Unlock Record’  would have been quite handy for this.

But with Process Builder introduced in Winter 15 release, you now have an easier and more elegant way of locking records. Here is a step-by-step guide with screenshots on how you can achieve the same.

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

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

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

    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.

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

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

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

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

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

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

Don't Miss Out on New Step-by-Step How To Guides on Salesforce...

…Subscribe to Our Newsletter!