Use Custom Metadata Types in Formulas, Default Values, and Validation Rules
- Describe use cases for referencing a custom metadata type in a default value, validation rule, or formula.
- Declaratively define a formula that references a custom metadata type.
- Declaratively define a formula that sets a default value.
- Declaratively define a validation rule that references a custom metadata type.
Custom metadata types can save you a lot of time, and if you pair them with other features, you can streamline your workflow processes even more. For example, formulas save time and work by automating calculations. Combined with custom metadata types, formulas eliminate the need for hardcoded values that you need to individually update.
Sound good? Let’s see how Acme Services can use custom metadata types in default values. Before continuing, if you need a refresher on creating custom objects and fields, review the Data Modeling module, 'cause we’re going to customize the Account object lickety-split.
Let’s start by adding a picklist field that stores information about support tiers to the Account object. Later, developers can map the picklist to our custom metadata type records, but for now let’s see how we can benefit from declarative features.
All customers receive support tiers. The most basic level of support is Bronze, so we want to use it as the default support tier. Customers can obtain higher levels of support, but they must spend more to receive those levels.
- In Setup, customize the Account object by adding a custom picklist field called Support Tier.
- Give Support Tier three options: Bronze, Gold, and Silver.
- Select Required.
- For Default Value, type: $CustomMetadata.Support_Tier__mdt.Bronze.MasterLabel
Why Reference Custom Metadata Types in Validation Rules?
Validation rules verify that the data that a user enters in a record meets the standards you specify before the user can save the rule. This verification helps to keep your data accurate and updated with the values that make sense. So, why can’t we use custom metadata types in validation rules?
Let’s return to our scenario, where we have support tiers of Gold, Silver, and Bronze. We want to ensure that an account gets a higher level support tier only after its spending level has reached a specified amount.
- Create another custom field on Account.
- For data type, enter Roll-Up Summary.
- For Field Label type Total Spending.
- For Summarized Object, select Opportunities.
- For Roll-Up Type, select Sum.
For Field to Aggregate, select Amount.
In Setup, go back to the Account object so that we can create our validation rule.
- Click Validation Rules and then click New.
- Call your rule Minimum Spending Validation.
You can copy and paste the following formula. However, before you do, notice that the rule
references custom metadata type records. You reference a custom metadata type record by
clicking Insert Field and traversing down to the field that you want to include in your
IF( (ISPICKVAL( Support_Tier__c , "Silver") && ( Total_Spending__c < $CustomMetadata.Support_Tier__mdt.Silver.Minimum_Spending__c ) ) || (ISPICKVAL( Support_Tier__c , "Gold") && ( Total_Spending__c < $CustomMetadata.Support_Tier__mdt.Gold.Minimum_Spending__c ) ), TRUE, FALSE )
- For error message, type Dollar value too low for this support tier.
- After you save your validation rule, create an account and an opportunity for that account. Name the account Beebs Publishing. Leave all defaults as they are.
- Now create an opportunity for Beebs Publishing and name it Beebs Publishing Q1. For Stage, select Closed-Won and for Close Date select the current date. Make sure to select Beebs Publishing as the account.
If we had multiple rules that checked the required minimum spending, we’d have to update all of the rules. By using custom metadata records within these validation rules, we update the values within the custom metadata record and don’t need to modify any of the validation rules.
Are you ready to take custom metadata types even further now? We hope so, because referencing them in formula fields is up next!
Reference a Custom Metadata Type Field in a Formula
Let’s say that we want to include information in the account record that shows how much more an account needs to spend to qualify for a higher support tier. We can do this by referencing our custom metadata type in a formula.
- In Setup, customize the Account object by adding a custom formula field called Amount Until Next Tier with a return type of Currency and 2 decimal places.
- In Enter Formula, select Advanced Formula.
Copy and paste this formula, which references a custom metadata type record:
IF ( Total_Spending__c < $CustomMetadata.Support_Tier__mdt.Bronze.Minimum_Spending__c, $CustomMetadata.Support_Tier__mdt.Bronze.Minimum_Spending__c - Total_Spending__c, IF ( Total_Spending__c < $CustomMetadata.Support_Tier__mdt.Silver.Minimum_Spending__c, $CustomMetadata.Support_Tier__mdt.Silver.Minimum_Spending__c - Total_Spending__c, IF ( Total_Spending__c < $CustomMetadata.Support_Tier__mdt.Gold.Minimum_Spending__c, $CustomMetadata.Support_Tier__mdt.Gold.Minimum_Spending__c - Total_Spending__c, 0 ) ) )
- Check the syntax and then click Next.
- Ensure that the field is visible to all profiles and then click Next.
Ensure that the field is added to Account layout and then click
Let’s see how this works.
- Return to the Account that you created.
- Change the Support Tier to Bronze.
- Edit the opportunity record for the account so that the Amount is $700.00.
- Return to the account and refresh your screen.
- In Amount Until Next Tier, you should see $300.00.
Imagine that you have a large org—or even a not-so-large org—and need to update several formula fields. Referencing a custom metadata type record is a great way to update all formulas at once. If you decide to package your custom metadata type, subscribers to the package can customize details, like price, while you still maintain the app logic.
Next, let’s see how custom metadata types can be handy in flows.