Use of custom metadata type in the validation rule
When you are working on an app (managed package) then validation rules are hard to maintain due to the limitation with the managed package components since you are not allowed to update the validation rule in the subscriber’s Org. Now, you don’t need to worry about that as Salesforce has enabled access to Custom Metadata Type values in validation rules.
When you have multiple validation rules across the object and they are using the same value then it’s always a tedious task to change the value in each and every validation rule. Storing that value in CMT can reduce your efforts dramatically.
After Spring ’18 release, you can use custom metadata type to store validation rule record values so no need to create another package for a change in the validation rule. Also, validation rules can be customer-centric if they are being used in an AppExchange product.
Example Custom Metadata Type in Validation Rule
ABC Corp wants to limit the users to change the stage of an opportunity record to “Cancelled” if the amount is more than $1000. This amount limit is being used in 3-4 more validation rules. Being a smart System Admin/Developer then you would prefer to use a custom metadata type over hard code values in 3-4 different places. You have to do following things in your Org.
- Create a custom metadata type. We are going to give it a name i.e. “OpportunitySetting“.
- Create a custom field of Number type and label it Cancel Amount Limit ( Cancel_Amount_Limit__c)
- Create a record and name it CancelAmount.
Now, you will have to use these values in the validation rule and a custom metadata type value can be referenced in a validation formula with the following syntax.
Our validation rule for the above example looks something like this:
AND( ISPICKVAL( StageName, "Cancelled" ), Amount > <span id="mce_SELREST_start" style="overflow:hidden;line-height:0;">﻿</span>$CustomMetadata.OpportunitySetting__mdt.CancelAmount.Cancel_Amount_Limit__c )
Make sure to use the correct suffix. As you can see in the above example –
__mdt for the custom metadata.
__c for the custom field and
Record doesn’t need a suffix.
Now, you have one more reason to use the custom metadata type in your Salesforce implementations. Happy to get feedback from you.