Creating efficient formulas can be an effective way to customize your Salesforce org. If you’re an expert in the field, you know that sometimes getting the formula right is the easy part. The challenge, however, can be found months down the road when you’re trying to understand how, and if, the formula works the way you think it does.
Not only does your formula have to work, but ideally, it's simple enough for you or someone else to understand later on. With help from our Sr. Salesforce Admin, we’ll share some steps you can take to simplify your Salesforce validation rules.
What is a Salesforce Validation Rule?
A validation rule is one of the tools we have at our disposal to ensure the accuracy and consistency of data within the Salesforce database. They allow us to describe what invalid configurations of data look like using simple, Excel-like formulas that are evaluated for records you are trying to save. Some common use cases for validation rules include:
- To verify that data is formatted in a particular way
- To conditionally require one field based on the value of another field
- To compare the values of two or more different fields on one record
How to Create a Salesforce Validation Rule
When creating a simple validation rule, it’s best to start with the value of a single field. You may start with something that looks like this:
After testing the rule for accuracy, you may realize that there’s another condition you need to check based on the value of another field. With the new addition, your formula may look like this:
NOT(FieldOne__c) || NOT(FieldTwo__c)
It’s easy to see how these formulas can get out of hand with the addition of each new field tacked onto the end. While there is nothing inherently wrong with creating validation rules this way, it can make things a bit more challenging to decipher in the long run.
How to Simplify Your Salesforce Validation Rules
Certain field naming conventions may cause confusion to others in your organization. For instance, your org may have Is_Subscribed_to_Newsletter__c instead of Is_Unsubscribed_from_Newsletter__c which can make it difficult when considering the overall truthiness or falseness of the formula.
In this case, there's a trick you can use to simplify your formula, without changing its truthiness. DeMorgan's Law, which describes how mathematical statements and concepts are related through their opposites, looks similar to distribution from algebra class. This law allows you to factor an ‘X’ out of an expression like (𝑥² +3𝑥), turning it into the mathematically equivalent 𝑥(𝑥+3), but with logical NOTs.
Let's convert our simple formula into a logical expression using some logic symbols:
Now the validation rule can change from:
NOT(FieldOne__c) || NOT(FieldTwo__c)
DeMorgan’s Law allows us to move the NOT outside of the parentheses if we're willing to change OR into AND, which will look like:
Depending on the situation, you may find this method easier to think about compared to the original.
It also works the other way around, and regardless of whether your expressions have the same ¬ value. For example, ¬(𝑃∨¬𝑄) could be converted into (¬𝑃∧𝑄).
Your mileage may vary, and whether one expression will be easier to understand than another one can be subjective. However, logical comparisons are used ALL the time in Salesforce, and it’s helpful to know how to restructure formulas to make them easier to understand.
Need some help?
If you'd like to learn more about additional Validation Rule tips, reach out to a member of our team today. We'd love to help you enhance your Salesforce experience with these tips and tricks!