Salesforce Custom Metadata Types Explained
In the world of Salesforce development and administration, managing configuration data effectively is crucial. From storing business rules to defining app behavior, developers need a reliable way to store and deploy metadata-driven settings. Salesforce Custom Metadata Types (CMDT) provide a powerful, scalable, and deployment-friendly solution for this purpose.
This comprehensive guide explores what Custom Metadata Types are, how they differ from Custom Settings, their key benefits, and how to implement them effectively in your Salesforce environment.
What Are Custom Metadata Types?
Custom Metadata Types (CMDT) are custom-defined metadata objects that allow you to create, manage, and deploy configuration records just like standard metadata (such as objects, fields, and workflows). Instead of storing runtime data, Custom Metadata Types store application-level configuration and logic that you can use across environments.
For example, you might define a CMDT to store tax rates by region, feature toggles, or integration settings that remain consistent across orgs.
Unlike Custom Settings or Custom Objects, CMDT data can be packaged, version-controlled, and deployed using tools like Change Sets or Salesforce CLI (SFDX).
Why Custom Metadata Types Matter
In traditional Salesforce setups, developers relied on Custom Settings or Custom Objects to store configuration data. However, these options had limitations:
-
Custom Settings could not easily be deployed with data.
-
Custom Objects required manual data migration between environments.
Custom Metadata Types solve both problems. They allow you to define configuration schemas and deploy both the structure and data between orgs seamlessly.
Key Use Cases
-
Storing business rules or mapping logic.
-
Managing integration configurations (like API keys or endpoints).
-
Defining field validation rules or dynamic picklist values.
-
Implementing feature flags for conditional behavior.
-
Managing dynamic UI visibility or process triggers.
Custom Metadata vs. Custom Settings vs. Custom Objects
To understand CMDT’s power, it’s important to compare it with other Salesforce data storage types.
| Feature | Custom Metadata Type | Custom Setting | Custom Object |
|---|---|---|---|
| Deployment via Metadata | ✅ Yes | ❌ No | ❌ No |
| Accessible via Apex | ✅ Yes | ✅ Yes | ✅ Yes |
| SOQL Query Required | ❌ No (use getAll()) | ❌ No (for Hierarchical) | ✅ Yes |
| Configuration Data | ✅ Ideal | ✅ Limited | ⚠️ Possible but not recommended |
| Packaged for ISVs | ✅ Yes | ❌ No | ⚠️ Partially |
| Data Stored as Metadata | ✅ Yes | ❌ No | ❌ No |
From this comparison, it’s clear that Custom Metadata Types combine the best of both Custom Settings and Custom Objects, making them the preferred choice for configuration-driven logic.
Components of Custom Metadata Types
A Custom Metadata Type is made up of two primary parts:
-
Metadata Type Definition: The schema that defines the fields (like an object).
-
Metadata Records: The individual configuration records created under the type.
For example, you can create a CMDT called Payment_Config__mdt with fields like:
-
Payment_Method__c -
Gateway_URL__c -
Is_Active__c
Then, create records such as PayPal Config or Stripe Config, which define values for these fields.
How to Create a Custom Metadata Type
Let’s go step-by-step on how to create and configure a CMDT in Salesforce.
Step 1: Create the Metadata Type
-
Go to Setup → Custom Metadata Types.
-
Click New Custom Metadata Type.
-
Enter:
-
Label: Payment Configurations
-
Object Name: Payment_Config
-
Description: Stores settings for payment gateway integrations.
-
-
Save the record.
Step 2: Add Custom Fields
-
Under Custom Fields, click New.
-
Create fields like:
-
Payment Gateway URL (Text)
-
API Key (Text)
-
Active (Checkbox)
-
-
Save your fields.
Step 3: Create Metadata Records
-
Go to Setup → Custom Metadata Types → Manage Records next to your type.
-
Click New Record and fill out your configuration values.
-
Create multiple entries for different configurations as needed.
Step 4: Access Custom Metadata in Apex
You can query Custom Metadata just like any other SObject, or access it using built-in methods.
Example: Using SOQL
Example: Using getAll()
The getAll() method retrieves all CMDT records without the need for SOQL queries, improving performance and avoiding governor limits.
Benefits of Using Custom Metadata Types
1. Easy Deployment
CMDTs can be deployed between orgs using metadata deployment tools, ensuring configuration consistency across environments.
2. Version Control Friendly
Since CMDT records are treated as metadata, they can be stored in version control systems like Git.
3. Performance Optimization
Unlike Custom Objects, CMDTs don’t consume data storage and can be accessed without query limits.
4. Secure and Configurable
CMDTs can be referenced in formulas, flows, and Apex without exposing sensitive information to end users.
5. Great for Managed Packages
ISVs (Independent Software Vendors) can package CMDTs with their apps to ensure out-of-the-box configuration deployment.
Using Custom Metadata in Automation
Custom Metadata Types can be leveraged not just in Apex but also in declarative tools.
In Flow
You can use CMDT records directly in Flow Builder using the “Get Records” element to dynamically drive decisions.
In Validation Rules and Formulas
CMDTs can be referenced in formulas, making your logic flexible and easier to maintain without hardcoding values.
Example formula:
This ensures that logic changes can be made without editing formulas every time.
CMDT Deployment Best Practices
-
Use Meaningful Names: Make sure your metadata type and record names are self-explanatory.
-
Version Control: Always track CMDT records in Git to maintain history and rollback options.
-
Limit Data Volume: CMDTs are not meant for storing large datasets—keep records minimal.
-
Avoid Hardcoding: Use CMDTs for dynamic logic instead of hardcoding values in Apex or Flow.
-
Test in Sandboxes: Always deploy CMDTs first in a sandbox to ensure they work correctly before pushing to production.
CMDT vs. Custom Labels
Although Custom Labels can store configurable text or keys, CMDTs provide more flexibility because they can store multiple fields and records with structured data.
Use Custom Labels for translatable UI text, and Custom Metadata Types for structured configuration data.
Real-World Example: Feature Toggle Management
Imagine you have multiple features in your Salesforce app, and you want to enable or disable them dynamically.
Step 1: Create CMDT
Create a Custom Metadata Type named Feature_Toggle__mdt with fields:
-
Feature_Name__c (Text)
-
Is_Enabled__c (Checkbox)
Step 2: Create Records
-
Feature: Email Notifications → Enabled
-
Feature: Auto Assignment → Disabled
Step 3: Reference in Apex
This setup allows you to enable or disable features without modifying code or redeploying packages.
Limitations of Custom Metadata Types
While CMDTs are powerful, they have a few constraints:
-
You cannot create them dynamically via Apex at runtime.
-
Limited record size (up to 1 MB total metadata size per org).
-
Cannot be modified by end users through standard UI pages (requires Setup access).
Conclusion
Salesforce Custom Metadata Types represent a major leap in how configuration data is handled across environments. They offer the flexibility of Custom Objects, the deployability of metadata, and the efficiency of system-level storage.
By using CMDTs, developers and admins can manage reusable configurations, simplify deployments, and ensure consistency across sandboxes and production. Whether you’re managing feature flags, integrations, or business logic, CMDTs are an indispensable tool in your Salesforce toolkit.
In essence, Custom Metadata Types help you build smarter, maintain cleaner, and deploy faster—making your Salesforce applications more scalable and future-proof.