Salesforce Custom Metadata Types Example: A Complete Guide
Salesforce Custom Metadata Types are a powerful feature that allows you to define and manage application configuration and metadata directly in Salesforce. Unlike regular custom objects, custom metadata types support versioning, packaging, and deployment, making them ideal for storing reusable, application-wide settings.
In this guide, we’ll demonstrate how to create and use custom metadata types with a practical example.
What Are Custom Metadata Types?
Custom Metadata Types are:
- Configuration Data: Used to store application settings that are metadata rather than transactional data.
- Deployable: Included in packages and change sets, making them easy to move between orgs.
- Accessible in Apex: Queryable directly in Apex without consuming SOQL limits.
Use Cases
- Storing default settings.
- Managing feature toggles.
- Defining business rules or mapping values.
Custom Metadata Types Example
Objective
We’ll create a custom metadata type to store tax rates for different states and use it to calculate tax in an Apex class.
Step 1: Create a Custom Metadata Type
- Navigate to Custom Metadata Types:
- Go to Setup → Search for Custom Metadata Types → Click New Custom Metadata Type.
- Define the Metadata Type:
- Label:
Tax Rate
. - Plural Label:
Tax Rates
. - Object Name:
Tax_Rate
. - Description: Stores tax rates for various states.
- Visibility: Set to
Public
.
- Label:
- Save the Metadata Type.
Step 2: Add Custom Fields
- Add Fields to the Metadata Type:
- Navigate to Fields & Relationships → New.
- Create Fields:
- State (Text, 100 characters): To store the state name or abbreviation.
- Tax Rate (Percent, 3 decimal places): To store the tax rate as a percentage.
Step 3: Add Metadata Records
- Navigate to Manage Records:
- Open the
Tax Rate
metadata type and click Manage Records.
- Open the
- Add Records:
- Record 1:
- Label:
California
. - State:
CA
. - Tax Rate:
7.25
.
- Label:
- Record 2:
- Label:
New York
. - State:
NY
. - Tax Rate:
8.88
.
- Label:
- Record 1:
Repeat for other states as needed.
Step 4: Use Custom Metadata in Apex
- Use custom metadata to retrieve tax rates and calculate tax.
- Use Developer Console or an Apex Test Class.
Test the Apex Class:
Decimal tax = TaxCalculator.calculateTax('CA', 1000);
System.debug('Tax for $1000 in CA: ' + tax);
Write an Apex Class:
public class TaxCalculator {
public static Decimal calculateTax(String state, Decimal amount) {
// Query the custom metadata type
List<Tax_Rate__mdt> taxRates = [SELECT State__c, Tax_Rate__c FROM Tax_Rate__mdt WHERE State__c = :state];
if (!taxRates.isEmpty()) {
Decimal taxRate = taxRates[0].Tax_Rate__c;
return (amount * taxRate) / 100;
} else {
throw new IllegalArgumentException('No tax rate found for the given state: ' + state);
}
}
}
Step 5: Deploy Custom Metadata
- Include in Change Set:
- Navigate to Outbound Change Sets → Add the custom metadata type and records.
- Deploy to Other Environments:
- Use change sets or packaging for seamless deployment.
Benefits of Custom Metadata Types
- No SOQL Limits:
- Access custom metadata in Apex without consuming SOQL queries.
- Version Control:
- Track changes and manage configuration across environments.
- Performance:
- Faster and more efficient than using custom objects for configuration data.
- Reusability:
- Define reusable settings that can be referenced across applications.
Common Use Cases
- Tax Rate Management:
- As demonstrated in this guide, custom metadata types can store tax rates for easy lookup.
- Feature Toggles:
- Enable or disable application features based on metadata values.
- Integration Configurations:
- Store API endpoints, authentication keys, or other integration details.
- Business Rules:
- Define and manage rules like discounts, commission rates, or SLAs.
Conclusion
Custom Metadata Types are a versatile feature in Salesforce, enabling organizations to manage configuration and application settings efficiently. This guide demonstrated how to create a custom metadata type to store tax rates and use it in an Apex class. By leveraging custom metadata types, you can build scalable, maintainable, and efficient solutions tailored to your business needs.