Hierarchy Custom Setting in Validation Rules

Introduction

According to Salesforce Help:

Validation rules verify that the data a user enters in a record meets the standards you specify before the user can save the record. A validation rule can contain a formula or expression that evaluates the data in one or more fields and returns a value of “True” or “False”. Validation rules also include an error message to display to the user when the rule returns a value of “True” due to an invalid value.

and:

Hierarchy Custom Settings is a type of custom setting that uses a built-in hierarchical logic that lets you “personalize” settings for specific profiles or users. The hierarchy logic checks the organization, profile, and user settings for the current user and returns the most specific, or “lowest,” value. In the hierarchy, settings for an organization are overridden by profile settings, which, in turn, are overridden by user settings.

When to use?

You have Validation Rules which have to be customized based on Profile or User. Because of that, you had to hard code Names and Id’s. That’s a bad practice!

Example:

Users should be prevented from changing the Stage after the Opportunity has been closed. Only System Administrators and one User should bypass the Validation Rule.

Screenshot of Validation Rule

Solution

Instead of hard coding Profile or User Names, Id’s, you need to configure a Hierarchy Custom Setting.

Configure Hierarchy Custom Setting (in Lightning Experience)

  • Go to Setup -> Custom Code -> Custom Settings.
  • Create new Hierarchy Custom Setting.
Screenshot of Custom Metadata
  • For each Validation Rule you want to bypass, you need to create a checkbox field (1). You can also create one checkbox field per object, if you want to bypass all Validation Rules (2).
  • In the Custom Fields section select: New -> Checkbox -> Enter Field Label, Field Name -> Save.
Screenshot of Custom Metadata
  • Click Manage and configure Validation Rule Configuration based on Profiles and Users.
    • Configure Default Organization Level Value Setting. Click New and check all checkboxes. It means that all Validation Rules are respected.
    • Configure settings for Profiles and Users. Click New in the section below the Default Organization Level Value. Choose Location and which Validation Rules will be respected by checking the checkboxes.
Screenshot of Custom Metadata

Update all Validation Rules which you want to bypass

Remove all hard coded Id’s and Names and add Hierarchy Custom Setting values.

Screenshot of Validation Rule

Validation Rules in Unit Tests

If you want to respect Validation Rules in Unit Tests, you need to insert Custom Setting values.

Example:
I want to fire Cannot_Change_Stage_After_Close Validation Rule in System Administrator context. In the TestSetup or before update the Opportunity you need to add:

Profile sysAdminProfile = [SELECT Id FROM Profile WHERE Name = 'System Administrator'];
insert new VRConfiguration__c(SetupOwnerId = sysAdminProfile.Id, Opp_Cannot_Change_Stage_After_Close__c = true);

Resources

4.5 2 votes
Article Rating
Subscribe
Notify of
guest
4 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Alex
1 year ago

I will have the ability to bypass the rule and edit Closed Opps with the Sys Admin profile if the Profile has Edit Closed Opps access, and I do not have any lower level, User Level, permissions in this setting.    Since I have user specific bypass access in the image below, but my user does NOT have Edit closed Opps, I will not be able to bypass the Edit Closed Opps Validation Rule.  The lowest level setting is used.

Patrick
1 year ago

Spring 18 actually allowed for Custom Metadata Types to be used in Validation Rules. I ve been working on a post about that as well. It will be out very soon. If you want to message me on Hangouts, Meighanrockssf or on Twitter @MeighanSF I m happy to talk with you about the topic before my post goes up. Hi Meighan, I would love to talk to you about it since I couldn t find a way of doing it using one VR line (for example $Setup.meighanrocks__VRS__c.meighanrocks__O_001_Edit_Closed_Opps__c = False), like you did using Custom Settings.

Solomon
1 year ago

Personally, I like to add a link to the Validation Rule in the Description and Help Text to make my life easier. i also explain what rights are granted when checked in the Help text  The default value is Unchecked.

Close Menu
4
0
Would love your thoughts, please comment.x
()
x