Skip to main content

Use Custom Metadata Types in Formulas, Default Values, and Validation Rules

Learning Objectives

After completing this unit, you’ll be able to:

  • 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.

Introduction

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. If you need a refresher on creating custom objects and fields, review the Data Modeling module, because we’re going to customize the Account object. 

Default Values

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 a support tier. 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.

  1. From Setup, go to the Object Manager tab and click Account.
  2. Click Fields & Relationships, then click New.
  3. Select Picklist, then click Next.
  4. For the Field Label, type Support Tier.
  5. Add three values for the picklist field: Bronze, Silver, and Gold.
  6. Select Required.
  7. For Default Value, type: $CustomMetadata.Support_Tier__mdt.Bronze.MasterLabel.
    Now when you create an Account record, the default value, Bronze, appears in Support Tier.
  8. Click Next, Next, and Save.
Note

There’s no UI for the Default Value option, so you can’t use the editor Select and Insert options when creating a custom metadata default value. The syntax is: $CustomMetadata.CustomMetadataTypeAPIName.RecordAPIName.FieldAPIName. Use the correct suffixes. For the custom metadata type, use __mdt. For fields, use __c. Records don’t require a suffix.

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 record. 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.

  1. From the Account details, Fields & Relationships page, click New.
  2. Select Roll-Up Summary, then click Next.
  3. For Field Label, type Total Spending, then click Next.
  4. For Summarized Object, select Opportunities.
  5. For Roll-Up Type, select SUM.
  6. For Field to Aggregate, select Amount.
  7. Click Next, Next, and Save.

Create a Validation Rule

Now we can create our validation rule. This validation rule returns an error if you select an account support tier and that account’s spending is less than what’s valid for that tier as defined in the Support Tier custom metadata type.

  1. From the Account page, click Validation Rules.
  2. Click New.
  3. For Rule Name, type Minimum Spending Validation.
  4. You can copy and paste the following error condition 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 validation rule.The insert field user interface, where we drill down to Minimum Spending.
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
)
  1. For Error Message, type Dollar value too low for this support tier, then click Save.

Check the Validation Rule

Now let’s check the validation rule by editing the Beebs Publishing account object.

  1. Create an account named Beebs Publishing. Leave all defaults as they are.
  2. Create an opportunity for the Beebs Publishing account and name it Beebs Publishing Q1.
    1. Select Beebs Publishing as the account.
    2. For Stage, select Closed Won.
    3. For Close Date, select the current date.
    4. Click Save.
  3. From the Beebs Publishing account, try to change the Support Tier to Gold. You should receive an error message.
  4. Go back to the Beebs Publishing Q1 opportunity and change Amount to $10,000.
  5. Now go to the Beebs Publishing account and edit Support Tier so that it’s Gold. You should be able to save the change.

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.

  1. From Setup, go to the Object Manager tab, then click Account.
  2. Click Fields & Relationships, then click New.
  3. Select Formula, then click Next.
  4. For the Field Label, type Amount Until Next Tier.
  5. For the Formula Return Type, select Currency.
  6. Set the Decimal Places to 2, then click Next.
  7. In Enter Formula, select the Advanced Formula tab.
  8. 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
              )
         )
    )

  9. Click Check Syntax, then click Next.
  10. Ensure that the field is visible to all profiles and then click Next.
  11. Ensure that the field is added to Account Layout and then click Save.

Check the Formula

Now let’s check the formula by editing the Beebs Publishing account object.

  1. Go to the Beebs Publishing account object you created.
  2. Change the Support Tier to Bronze.
  3. Edit the opportunity record for the account so that the Amount is $700.00.
  4. Return to the account and refresh your screen. In Amount Until Next Tier, you should see $300.00. If you don’t see the field, click Edit, and you see the field there.

A portion of an Account detail page with the Amount Until Next Tier field.

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.

Resources

Keep learning for
free!
Sign up for an account to continue.
What’s in it for you?
  • Get personalized recommendations for your career goals
  • Practice your skills with hands-on challenges and quizzes
  • Track and share your progress with employers
  • Connect to mentorship and career opportunities