Sub topic 10
Linking types with metadata relationship fields
This chapter assumes you can define a custom metadata type and its fields, create records, and query them with SOQL. It covers the metadata relationship field, which lets one custom metadata record point to another record, an object, or a field.
Why a special relationship field
Custom metadata records are deployed between orgs, and record IDs differ in each org. A normal lookup would break on deployment. A metadata relationship field stores a stable name instead. Salesforce also checks the link, so you cannot delete a record, object, or field that another record still references.
A metadata relationship field can point to one of three targets:
| Target | Example value stored | Typical use |
|---|---|---|
| Another custom metadata type | A record of `Feature_Flag__mdt` | Parent and child configuration |
| An object (Entity Definition) | `Account` | Configuration that applies to one object |
| A field (Field Definition) | `Account.Industry` | Configuration that applies to one field |
Create the field
Suppose each feature flag can have audience rules. You want many Feature_Flag_Rule__mdt records per Feature_Flag__mdt record.
- In Setup, open Custom Metadata Types and select
Feature_Flag_Rule. - Under Custom Fields, click New.
- Choose Metadata Relationship as the data type.
- Pick the related entity, here
Feature Flag (Custom Metadata Type). - Name the field
Feature_Flag, so the API name isFeature_Flag__c.
When you create a rule record, the editor shows a lookup limited to existing flag records.
Read related records
Use SOQL and traverse the relationship with __r, as you would for a lookup on a custom object.
String flagName = 'New_Checkout';
List<Feature_Flag_Rule__mdt> rules = [
SELECT DeveloperName, Audience__c, Feature_Flag__r.DeveloperName,
Feature_Flag__r.Is_Enabled__c
FROM Feature_Flag_Rule__mdt
WHERE Feature_Flag__r.DeveloperName = :flagName
];
for (Feature_Flag_Rule__mdt rule : rules) {
System.debug(rule.DeveloperName + ' applies to ' + rule.Audience__c
+ ', flag enabled: ' + rule.Feature_Flag__r.Is_Enabled__c);
}The query returns each rule together with its parent flag values in one statement, so you do not need a second query per flag.
Point to an object or field
A relationship to an object or field lets configuration name the thing it applies to without a free-text string. Imagine Validation_Setting__mdt with a field Target_Object__c that points to an object. The stored value is the object's API name, so you can use it with Schema methods.
Validation_Setting__mdt setting = Validation_Setting__mdt.getInstance('Account_Rules');
String objectName = (String) setting.Target_Object__c;
Schema.SObjectType targetType = Schema.getGlobalDescribe().get(objectName);
System.debug('Rules apply to ' + targetType);Check for null before you use the describe result. The value is only as current as the configuration, and a rule can still refer to an object that a later deployment removed in another org.
Design notes
- Use a relationship field instead of a text field whenever the value must match an existing record, object, or field. Salesforce enforces the match.
- The child record can only be deployed after its parent, or in the same deployment. Include both in one change set or package.
- Keep hierarchies shallow. Two levels, such as flag and rule, are easy to query and reason about.
Exercise
Exercise
A Discount_Rule__mdt record must belong to one Discount_Tier__mdt record, and the link must survive deployment to a sandbox with different record IDs. Which field type do you add to Discount_Rule__mdt, and how do you read the tier's Percent__c in SOQL?
Show answer
Add a Metadata Relationship field, for example Discount_Tier__c, that targets Discount Tier (Custom Metadata Type). Query it through the __r name: SELECT DeveloperName, Discount_Tier__r.Percent__c FROM Discount_Rule__mdt. The link uses the developer name, not an ID, so it survives deployment.