Custom Settings and Validation Rules

I’ve had this same conversation several times over the past month, so thought I’d make a post about it. This isn’t a new trick and there are probably dozens of posts on this same topic. Hopefully I can add a little bit of new information.

Custom Settings are hidden under the Develop section of Setup so many people aren’t even aware they are there. I’ll be covering hierarchical custom settings in this post. A hierarchical custom setting allows you to set values at the org, profile and user levels. When a custom setting is requested for a particular user, Salesforce returns the lowest level available.

Consider these two use cases: you are doing a big data load and validation rules would prevent the data to be loaded. You have an object that a lot of users have edit rights on, but you only want a subset of users to be able to make changes to a particular field. We can use custom settings to help us out.

1. Create a new custom setting. Note that you want the Setting Type to be Hierarchy. Visibility doesn’t matter unless you are including the custom setting in a managed package.

Creating a Custom Setting

2. Add a field to the custom setting. This will be used to bypass all the validation rules.

Creating a Custom Setting Field

3. Add values at the different hierarchy levels you need. Let’s first just add an org-wide one to require all validation rules to work. To add values, you click on the “Manage” link for the custom setting and then click on the “New” button.

4. Update your validation rules. Let’s say we have a validation rule that requires Accounts to have an Annual Revenue greater than $1,000. Our validation rule will look something like this:

!$Setup.ValidationsRules__c.BypassRules__c &&   AnnualRevenue <1000

The first part of this validation rule checks to see if we can bypass the validation rules and the second part is the actual rule.

Now you can add a hierarchy level to the custom setting at the profile or user level and check the box to allow those users to save records even if their Annual Revenue is less that $1000.

To meet the other need to only allow some users to edit specific fields, the concept is the same: create a new custom setting, add a field and then set the values as needed. Your validation rule would look something like this:

!$Setup.ValidationsRules__c.BypassRules__c && IsChanged(AnnualRevenue)

A couple of things to note:

  1. Use a different custom setting for each of these scenarios. If you use the same one with multiple fields, it will be harder to keep track of who has what defaults.
  2. You can also use custom settings in formula fields!

How do you like to use custom settings in the admin world?

2 thoughts on “Custom Settings and Validation Rules”

Leave a Reply