Use Metadata Relationship Fields
Learning Objectives
- Use metadata type relationship fields when needed.
- Declaratively define a custom metadata type with a relationship field that references another custom metadata type, the entity definition, or a field definition.
- Add records to custom metadata types that have relationship fields.
Introduction
As you already learned in Custom Metadata Types Basics, the flexibility and declarative options of the custom metadata types feature save you time. Now let’s see how we can use metadata relationship fields to look up records of other custom metadata types, entity definitions, or field definitions.
Metadata relationship fields work like relationship fields for standard or custom objects. But because they are custom metadata types, they have the same benefits of application configuration data—the best of both worlds, which is nice because our customers like flexibility. Let’s check it out.
Our Use Case
In the previous module, we created a custom metadata type to store configurations for different support tiers, such as minimum spending required to qualify for a specified tier and a default discount that is provided for each tier. We also created a picklist field on Account to indicate what support level a particular account qualifies for. This solution can work for a smaller organization, but what if you have a larger company with more complicated requirements? For example, if the support tier names in the picklist field on Account are different from the support tier names in the custom metadata type records, then we can't map the picklist values to the custom metadata type records. And if we include another object with support tiers, it becomes even more complicated.
- Relationship to another custom metadata type
- Relationship to an EntityDefinition (such as a custom or a standard object definition)
- Relationship to a FieldDefinition (for both custom and standard fields)
The Big Picture
Goal | What we’ll do |
---|---|
Store information about available support products. | Create a custom object called Support Product.* |
Map support tier configurations to support product requirements. | Create a custom metadata type called Support Level Mapping, with three metadata relationship fields. |
Create a Custom Object and Fields
Let’s create the custom object and fields that use metadata relationships. For a refresher on custom object and field creation, go to the Data Modeling module.
- In Setup, create a custom object called Support Product.
- Scroll down to the bottom of the creation page and select Launch New Custom Tab Wizard after saving this custom object, then click Save.
- Select a tab style and click Next.
- Make sure that the tab visibility is set to Default On for all profiles and click Next, then click Save.
- In Setup, customize the Support Product object and add a custom picklist field called Support Level.
- Define three values for the picklist field: Free, Premium, and VIP.
- Add support product records. Make sure to enter data in the Support Product name and the Support Level fields.
Support Product | Support Level |
---|---|
Dedicated Phone Line | VIP |
Online Chat | Premium |
Email Support | Free |
Create a Custom Metadata Type with Relationship Fields
Create a custom metadata type named Support Level Mapping and add four fields.
- From the Support Level Mapping detail page, scroll to the Custom Fields section and click New.
- Choose Metadata Relationship for the field type and click Next.
- In the Related To dropdown list, select Entity Definition and click Next.
- Enter Support Tier Object for the field label and Support_Tier_Object for the field name.
- Click Next and then Save.
Field Label | Field Name | Field Type | Other Options |
---|---|---|---|
Support Tier Object | Support_Tier_Object | Metadata Relationship | Related To: Entity Definition |
Support Tier Field | Support_Tier_Field | Metadata Relationship | Related To: Field Definition. Controlling Field: Support Tier Object |
Support Tier | Support_Tier | Metadata Relationship | Related To: Support Tier (custom metadata type) |
Support Tier Name | Support_Tier_Name | Text |
Create Custom Metadata Type Records
Add a record to the Support Level Mapping custom metadata type.
- In the Support Level Mapping detail page, click Manage Support Level Mappings.
- Click New.
- Label your record Free - Support Product.
- For Support Tier Object, select Support Product.
- For Support Tier Field, enter Support Level.
- For Support Tier, select Bronze.
- For Support Tier Name, enter Free.
- Click Save.
The Free - Support Product record maps the Free value (in the Support Tier picklist on the Support Product custom object) to the Bronze support tier.
Add mappings for the Premium and VIP tier products.
Now the relationships can be used within your Apex code to provide an even more enhanced user experience for Acme Services.
Resources
- Trailhead Module: Custom Metadata Types Basics
- Salesforce Help: Custom Metadata Relationships
- Developer Documentation: Force.com Tooling API > Introducing Tooling API
- Developer Documentation: Force.com Tooling API > EntityDefinition