back to resources
RECORD LOCK IN SALESFORCE
August 26 2022 • 20 min read

BUSINESS CHALLENGE

Depending on the requirement we may have to prevent a record from being edited whenever a criteria has been met.

rlb 36

Here we are going to prevent an Opportunity from being edited when the stage reaches Closed Lost or Closed Won since we do not want the users to change the record data after it was closed.

We explain two of the several ways on how to lock the record.

  1. Locking the record using Approval Process
  2. Locking the record using Page Layout and Record Types

LOCKING THE RECORD USING APPROVAL PROCESS

STEPS TO ACHIEVE THE REQUIREMENT

  1. Create Approval Process
  2. Create a Flow

CREATE APPROVAL PROCESS

  1. In Order to learn how to create an Approval Process, please visit our blog Approval Process in Salesforce.

  2. We have created an Approval Process where the entry criteria is Stage equals to Closed Won or Closed Lost.

    rlb 1

  3. The below screenshot shows how the  Approval Process for record lock looks like. 

    rlb 2

  4. Include only a single step for the Approval Process and define the step criteria as All records should enter this step. Add any System admin as the Assigned Approver and save the step.

  5. Record Lock in Initial Submission actions is a default action provided by Salesforce, whenever the record is submitted for Approval, the record will be locked and will prevent the users from editing it.

  6. In Final Approval Actions, Click Add New and Select Unlock the Record for Editing option. Click save

  7. In any circumstances if the user wants to edit the record again, the user needs to request the system admin to approve that record so that the record will be unlocked for editing again.

  8. Activate the Approval Process.

CREATE A FLOW

rlb 3

  1. Click Gear Icon → Click Setup.

    rlb 4

  2. In Quick Find Box Search and Select Flow.

  3. Click New Flow.

  4. Select Record Triggered Flow → Click Create

    rlb 5

  5. In the start element choose Opportunity as the object and under Configure Trigger choose A record is created or updated to trigger the flow.

    rlb 6

  6. Under Set Entry conditions choose Any Condition is Met(OR) as the Condition Requirements.

  7. Choose StageName as the field, Equals as the Operator and Closed Won as the Value. Similarly add another condition and make the field value as Closed Lost.

    rlb 7

  8. Under When to Run the flow for updated Records choose Only when a record is updated to meet the condition requirements.

  9. Optimize the flow for Actions and Related Records. This flow runs after the record has been saved to the database.

    rlb 8

  10. Click Done.

  11. Add an Action Element and Search and Select Submit for Approval. Enter the label as Trigger Approval Process. API Name will be automatically populated.

    rlb 9

  12. Under Set Input Values choose Opportunity Id as the record Id.

    rlb 10

  13. For Approval Process Name or ID Enter “Record_Lock_Process” which is the API Name of the Approval Process.

    rlb 11

  14. Click Done.

  15. Save the Flow and Enter Flow Label as Record Lock Flow.

  16. Activate the Flow.

TESTING THE PROCESS

When the stage of an Opportunity is changed to Closed Won, the flow will be triggered automatically and submits the record for Approval.

Before Changing the stage → No Approval Process.

rlb 12

After Changing the stage to Closed Won (or) Closed Lost → Record enters the Approval Process.

rlb 13

When the user tries to edit the record it displays an error This record is locked. if you need to edit it, contact your admin.

rlb 14

LOCKING THE RECORD USING RECORD TYPES AND PAGE LAYOUT

STEPS TO ACHIEVE THE REQUIREMENT

  1. Create a Page Layout
  2. Create Record Type
  3. Create a Validation Rule
  4. Create Flow

CREATE A PAGE LAYOUT

  1. Click Gear Icon → Click Set Up.

    rlb 15

  2. In Quick Find Box → Search and Select Opportunity object.

  3. Select Page Layouts → Click New.

    rlb 16

  4. Choose an existing page layout which you want to lock and Enter Lock Page Layout as the Page Layout Name.

    rlb 17

  5. Click Save

  6. After clicking save it will open the newly created page layout.

    rlb 18

  7.  Hover the cursor over any field and you will see a wrench icon(), , click on it and check the Read-only option. Repeat the same for all the fields on the page layout. The Opportunity Name, Stage Name and Close date cannot be made read-only. Please refer to the link for more information).

    rlb 19

  8. All the fields that have been read only will have a lock icon displayed before them.

    rlb 20

  9. Click Save Button to save the page layout.

    rlb 21

CREATE RECORD TYPE

  1. Select Record Types in Opportunity Object, Click New.

    rlb 22

  2. Choose the required record type for Existing Record Type, Enter the Record Type Label as Locked Record Type and the Record Type Name will be automatically populated. Choose any of the Sales Process and Check the active option to make the record type active after saving.

    rlb 23

  3. Scroll down and you will see the profile assignment. Here we make the record type unavailable(unCheck) for all the Profiles.

    rlb 24

  4. Click Next.

  5. In the Page Layout Assignment step, let the radio button be on the Apply one layout to all profiles and select Lock Page Layout in the dropdown next to it.

    rlb 25

  6. Click Save. Head to the Record Types in Opportunity Object settings and click on the Record Type and copy the RecordTypeId from the top of the page.

CREATE  A VALIDATION RULE

  1. Since the three fields Opportunity Name, Stage and Close Date cannot be made read-only in the page layout, we create a validation rule to prevent the user from editing the fields.

  2. Select Validation Rules in the opportunity object, Click New.

    rlb 26

  3. Enter Rule Name as “Rule_for_name_stage_and_closed_date

  4. Enter the below mentioned formula in the formula screen.

    AND(RecordType.DeveloperName = "Locked_Record_Type", OR(ISCHANGED(StageName),ISCHANGED(Name),ISCHANGED(CloseDate)))

    rlb 27

  5. In Error Message type “Record is Locked - Record cannot be edited” 

  6. Select Error Location as Top of Page.

    rlb 28

  7. Click Save.

CREATE FLOW

  1. From setup, In quick find box search and select Flow.

  2. Click New Flow.

  3. Select Record Triggered Flow, Click Create.

    rlb 29

  4. In the Start Element choose Opportunity as the Object. Under Configure Trigger choose A record is updated as the option to trigger the flow.

  5. Under Set Entry conditions choose Any Conditions is Met(OR) as the condition requirements. For the condition choose StageName as the field, Equals as operator and Closed Won as the Value. Similarly add another condition to make the stage name equals to Closed Lost.

  6. Choose Only when a record is updated to meet the condition requirements as the option for When to Run the Flow for Updated Records.

  7. Optimize the flow for Actions and Related Records

    rlb 30

  8. Click Done.

  9. Click the (+) icon and Select Update Triggering Record.

    rlb 31

  10. Enter the label as Update Record Type, API Name will be populated automatically. For the option How to Find the Records to Update and set their values choose Use the opportunity record that triggered the flow.

  11. Under Set Filter Conditions choose None–Always Update Record as the Condition Requirements to Update Record. Under Set Field Values for the Opportunity Record choose RecordTypeId as the Field and Value as the copied Record Type Id.

    rlb 32

  12. Click Done.

  13. Enter the label as Record Lock by Page Layout and the API Name will be populated automatically.

    rlb 33

  14. Activate the Flow

TESTING THE PROCESS

Here the record is in the Proposal/Price Quote stage. Users will be able to see the edit icon for fields.

rlb 34

Now When the stage is changed from Profile/Price Quote to Closed Won, the record type automatically changes.

rlb 35

Now the edit icon will not visible for all the fields except the three(Name, Stage and Closed Date)

If you try to edit those fields, the Validation rule will trigger and prevent the user from editing the record.

rlb 36

WRAPPING IT UP

In this Blog we have covered how to lock a record in two different ways when certain criteria is met.

Leave a Comment

Your email address will not be published

© 2022 Digital Biz Tech