BUSINESS CHALLENGE
Depending on the requirement we may have to prevent a record from being edited whenever a criteria has been met.
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.
- Locking the record using Approval Process
- Locking the record using Page Layout and Record Types
LOCKING THE RECORD USING APPROVAL PROCESS
STEPS TO ACHIEVE THE REQUIREMENT
- Create Approval Process
- Create a Flow
CREATE APPROVAL PROCESS
-
In Order to learn how to create an Approval Process, please visit our blog Approval Process in Salesforce.
-
We have created an Approval Process where the entry criteria is Stage equals to Closed Won or Closed Lost.
-
The below screenshot shows how the Approval Process for record lock looks like.
-
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.
-
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.
-
In Final Approval Actions, Click Add New and Select Unlock the Record for Editing option. Click save
-
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.
-
Activate the Approval Process.
CREATE A FLOW
-
Click Gear Icon → Click Setup.
-
In Quick Find Box Search and Select Flow.
-
Click New Flow.
-
Select Record Triggered Flow → Click Create
-
In the start element choose Opportunity as the object and under Configure Trigger choose A record is created or updated to trigger the flow.
-
Under Set Entry conditions choose Any Condition is Met(OR) as the Condition Requirements.
-
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.
-
Under When to Run the flow for updated Records choose Only when a record is updated to meet the condition requirements.
-
Optimize the flow for Actions and Related Records. This flow runs after the record has been saved to the database.
-
Click Done.
-
Add an Action Element and Search and Select Submit for Approval. Enter the label as Trigger Approval Process. API Name will be automatically populated.
-
Under Set Input Values choose Opportunity Id as the record Id.
-
For Approval Process Name or ID Enter “Record_Lock_Process” which is the API Name of the Approval Process.
-
Click Done.
-
Save the Flow and Enter Flow Label as Record Lock Flow.
-
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.
After Changing the stage to Closed Won (or) Closed Lost → Record enters the Approval Process.
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.
LOCKING THE RECORD USING RECORD TYPES AND PAGE LAYOUT
STEPS TO ACHIEVE THE REQUIREMENT
- Create a Page Layout
- Create Record Type
- Create a Validation Rule
- Create Flow
CREATE A PAGE LAYOUT
-
Click Gear Icon → Click Set Up.
-
In Quick Find Box → Search and Select Opportunity object.
-
Select Page Layouts → Click New.
-
Choose an existing page layout which you want to lock and Enter Lock Page Layout as the Page Layout Name.
-
Click Save.
-
After clicking save it will open the newly created page layout.
-
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).
-
All the fields that have been read only will have a lock icon displayed before them.
-
Click Save Button to save the page layout.
CREATE RECORD TYPE
-
Select Record Types in Opportunity Object, Click New.
-
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.
-
Scroll down and you will see the profile assignment. Here we make the record type unavailable(unCheck) for all the Profiles.
-
Click Next.
-
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.
-
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
-
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.
-
Select Validation Rules in the opportunity object, Click New.
-
Enter Rule Name as “Rule_for_name_stage_and_closed_date”
-
Enter the below mentioned formula in the formula screen.
AND(RecordType.DeveloperName = "Locked_Record_Type", OR(ISCHANGED(StageName),ISCHANGED(Name),ISCHANGED(CloseDate)))
-
In Error Message type “Record is Locked - Record cannot be edited”
-
Select Error Location as Top of Page.
-
Click Save.
CREATE FLOW
-
From setup, In quick find box search and select Flow.
-
Click New Flow.
-
Select Record Triggered Flow, Click Create.
-
In the Start Element choose Opportunity as the Object. Under Configure Trigger choose A record is updated as the option to trigger the flow.
-
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.
-
Choose Only when a record is updated to meet the condition requirements as the option for When to Run the Flow for Updated Records.
-
Optimize the flow for Actions and Related Records
-
Click Done.
-
Click the (+) icon and Select Update Triggering Record.
-
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.
-
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.
-
Click Done.
-
Enter the label as Record Lock by Page Layout and the API Name will be populated automatically.
-
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.
Now When the stage is changed from Profile/Price Quote to Closed Won, the record type automatically changes.
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.
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