進行状況の追跡を始めよう
Trailhead のホーム
Trailhead のホーム

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

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

  1. In Setup, customize the Account object by adding a custom picklist field called Support Tier.
  2. Give Support Tier three options: Bronze, Gold, and Silver.
  3. Select Required.
  4. 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.
Note

Note

There is no UI for this 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 do not require a suffix.

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.

  1. Create another custom field on Account.
  2. For data type, enter Roll-Up Summary.
  3. For Field Label type Total Spending.
  4. For Summarized Object, select Opportunities.
  5. For Roll-Up Type, select Sum.
  6. For Field to Aggregate, select Amount.
    In Setup, go back to the Account object so that we can create our validation rule.
  7. Click Validation Rules and then click New.
  8. Call your rule Minimum Spending Validation.
  9. 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 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
    )
    
  10. For error message, type Dollar value too low for this support tier.
  11. 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.
  12. 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.
Return to the account you created, and edit it. Try to change the Support Tier to Gold. You should receive an error message. Edit the opportunity amount to $10,000. Now go to the account and edit Support Tier so that it’s Gold. You should be able 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. 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.
  2. In Enter Formula, select Advanced Formula.
  3. 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
              )
         )
    )
    
  4. Check the syntax and then click Next.
  5. Ensure that the field is visible to all profiles and then click Next.
  6. Ensure that the field is added to Account layout and then click Save.
    Let’s see how this works.
  7. Return to the Account that you created.
  8. Change the Support Tier to Bronze.
  9. Edit the opportunity record for the account so that the Amount is $700.00.
  10. Return to the account and refresh your screen.
  11. In Amount Until Next Tier, you should see $300.00.
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.

retargeting