Custom Metadata Types in Salesforce enable admins and developers to store and manage configuration data that is deployable across environments. Unlike Custom Settings, Custom Metadata Types allow both their structure and records to be included in deployments, making them ideal for maintaining consistency in org configurations.
This guide covers:
- What Custom Metadata Types Are
- How They Work
- Common Use Cases
- Benefits of Using Custom Metadata Types
- Limitations and When Not to Use Them
- Bonus Use Case: Dynamic Email Recipients Based on Business Rules
What Are Custom Metadata Types?
Custom Metadata Types (CMTs, also sometimes MDTs) are specialized objects designed to store configuration data for an application. They behave similarly to Custom Objects but are optimized for configuration scenarios.
Key Features:
- Both metadata definitions and records can be deployed across orgs.
- Data is cached for fast access in Apex, validation rules, Flows, and formulas.
- Ideal for storing static or reusable configuration data.
How Do Custom Metadata Types Work?
1. Creating a Custom Metadata Type
- Define a Custom Metadata Type to structure your configuration data.
- Add fields to hold relevant data, such as thresholds, conditions, or values.
2. Populate Records
- Add records to the Custom Metadata Type to store specific configuration data (e.g., rules for different regions or business processes).
3. Reference in Salesforce Configurations
- Retrieve data programmatically in Apex or declaratively in formulas, validation rules, and flows.
Example Apex Usage:
MyMetadataType__mdt record = MyMetadataType__mdt.getInstance('RecordName');
System.debug('Threshold: ' + record.Threshold__c);
Example Formula Usage:
$CustomMetadata.MyMetadataType__mdt.RecordName.FieldName__c
Common Use Cases for Custom Metadata Types
1. Feature Toggles
Enable or disable specific functionality in Salesforce dynamically:
- Field:
FeatureName__c
(Text) - Field:
IsEnabled__c
(Checkbox)
Example Apex Logic:
if (FeatureToggle__mdt.getInstance('NewFeature').IsEnabled__c) {
// Execute feature logic
}
2. Routing Records to Owners
Store an object's routing rules based on attributes like region or lead source:
- Field:
Region__c
(Text) - Field:
LeadSource__c
(Picklist) - Field:
OwnerId__c
(Text) - Custom metadata does not support lookups, so you must enter the ID of the user. Recommended to also have a column for user's Name so you can easily know who the ID corresponds to.
Example Flow:
- Query the Custom Metadata Type to dynamically assign leads. Query based on matching attributes in the fields on your custom metadata type, then return the ownerId corresponding to that record that was retrieved. Use this to assign ownership in Flow or code.
3. Validation Rule Thresholds
Store thresholds for validation rules:
- Field:
Threshold__c
(Number) - Field:
ValidationMessage__c
(Text)
Example Validation Rule: Have the 'Amount' threshold be dynamic based on different industries. In this example, Healthcare and Technology industries pull their thresholds from the corresponding custom metadata records, and if the Industry is something different, it uses the default record's threshold.
ISPICKVAL(StageName, 'Closed Won') &&
Amount >
CASE( Industry,
"Healthcare", $CustomMetadata.ValidationRules__mdt.Healthcare_Threshold.Threshold__c,
"Technology", $CustomMetadata.ValidationRules__mdt.Technology_Threshold.Threshold__c
$CustomMetadata.ValidationRules__mdt.Default_Threshold.Threshold__c
)
4. Dynamic Email Recipients
Custom Metadata Types can be used to store rules for determining dynamic email recipients based on criteria like region, vertical, or dollar amount thresholds. This ensures that the right stakeholders are notified without hardcoding recipient lists.
Use Case:
Send opportunity notifications to different email recipients based on:
- Region: North America, EMEA, APAC.
- Vertical: Healthcare, Technology, Retail.
- Dollar Amount Thresholds: Notify executives for deals over $1M.
Fields in the Custom Metadata Type:
Field Name | Data Type | Description |
---|---|---|
Region__c | Text | The region (e.g., NA, EMEA, APAC). |
Vertical__c | Text | The vertical (e.g., Healthcare, Tech). |
Threshold__c | Number | Minimum opportunity amount for alerts. |
EmailRecipients__c | Text | Comma-separated list of email addresses. |
Example Record:
Region | Vertical | Threshold | EmailRecipients |
---|---|---|---|
NA | Healthcare | 1000000 | john.doe@example.com,jane.doe@example.com |
EMEA | Technology | 500000 | emea@example.com |
Example Apex:
public class OpportunityEmailNotifier {
public static void notifyRecipients(Opportunity opp) {
// Query matching metadata record
List<EmailRecipients__mdt> rules = [
SELECT Region__c, Vertical__c, Threshold__c, EmailRecipients__c
FROM EmailRecipients__mdt
WHERE Region__c = :opp.Region__c AND
Vertical__c = :opp.Vertical__c AND
Threshold__c <= :opp.Amount
LIMIT 1
];
if (!rules.isEmpty()) {
EmailRecipients__mdt rule = rules[0];
List<String> emails = rule.EmailRecipients__c.split(',');
// Send email logic here
System.debug('Emails to notify: ' + emails);
}
}
}
Benefits of Custom Metadata Types
- Deployable Configuration:
- Both metadata structure and records can be deployed, ensuring consistent configurations across orgs.
- No SOQL Limit:
- Accessing data does not consume SOQL queries or CPU time.
- Reusable and Scalable:
- Ideal for scenarios requiring configurable, reusable logic.
- Flexible Use Cases:
- Custom Metadata Types support a variety of business needs, from feature toggles to routing logic.
Limitations of Custom Metadata Types
- No Relationships:
- Custom Metadata Types cannot have lookup or master-detail relationships.
- Static Data:
- Records cannot be modified programmatically during runtime.
- Storage Limits:
- Limited to 10MB per org across all Custom Metadata records.
Best Practices
- Plan for Maintenance:
- Regularly review Custom Metadata records to ensure relevance.
- Use Naming Conventions:
- Use clear, descriptive names for fields and records.
- Combine with Other Tools:
- Use Custom Metadata for logic and Custom Labels for multilingual text.
- Document Use Cases:
- Maintain clear documentation of how each Custom Metadata Type is used in your org.
Conclusion
Custom Metadata Types are a powerful tool for creating scalable, maintainable, and reusable solutions in Salesforce. Whether you’re managing feature toggles, validation thresholds, email recipients, or integration configurations, Custom Metadata Types allow you to centralize and deploy logic dynamically without hardcoding.
By leveraging Custom Metadata Types thoughtfully, you can build more agile and robust Salesforce configurations that adapt to evolving business needs.
Comments