💬"Imagine updating your org’s logic without changing a single line of code. That’s the power of Custom Metadata Types."
Salesforce is all about flexibility. But if you're hardcoding values in Apex or creating dozens of formula fields for every business rule, you're missing a game-changing feature: Custom Metadata Types (CMDT).
In this blog, we’ll break down everything you need to know to start using CMDT to build dynamic, flexible, and admin-friendly business logic—the smart way.
What is Dynamic Business Logic?
Dynamic Business Logic means building systems where rules, thresholds, values, or decision paths can be changed without touching code or redeploying.
Example:
Changing the discount % by customer type.
Instead of coding this value, store them as metadata that admins can manage.
What Are Custom Metadata Types?
Custom Metadata Types (CMDT) are like custom objects but designed to store configuration and logic data — not transactional data.
Key Features:
1. Fully deployable via change sets or packages
2. Accessible in Apex, Validation Rules, and Flows
3. No DML required (read-only, but fast and cacheable)
4. Managed via Setup (admin-friendly)
Step-by-Step: Create Custom Metadata for Business Logic
Use Case: Discount by Customer Tier
Problem:
In many Salesforce implementations, discount values based on customer tiers (e.g., Gold, Silver, Bronze) are hardcoded into Apex classes or Flows. This makes it difficult for admins to update discount logic without involving developers, risking delays and potential deployment issues.
Solution:
Use Custom Metadata Types (CMDT) to store and manage discount percentages for each customer tier. This allows you to dynamically apply discounts based on the Account's Tier__c field. Admins can easily update discount values directly in the Setup UI—no code changes or deployments required.
Result:
Business logic becomes configurable, scalable, and admin-friendly,enabling faster updates and better alignment with business needs—all while maintaining clean, reusable code.
Let’s learn how to set up a Custom Metadata Type in order to solve the problem.
Step 1: Create the Custom Metadata Type
1. Go to Setup → Search Custom Metadata Types.
2. Click New Custom Metadata Type.
3. Label: Customer Tier Discount.
4. Object Name: Customer_Tier_Discount.
5. Enable: “Visible to all users”.
Step 2: Add Fields to the CMDT
Click Manage Records → New Field for each:
Field Label |
Field Type |
API Name |
Tier Name |
Text |
Tier_Name__c |
Discount (%) |
Number |
Discount__c |
Similarly, you can add the second field.
Step 3: Add CMDT Records (Your Logic)
Click Manage Records → New
Create:
1. Tier Name: Gold | Discount: 20.
2. Tier Name: Silver | Discount: 10.
3. Tier Name: Bronze | Discount: 5.
Similarly, you can add as much as you want.
Now you can use the Custom Metadata Type created above in apex class to retrieve the discount value.
Best Practices for CMDT
1. Use naming conventions (Rule_Config__mdt, Mapping__mdt).
2. Keep descriptions updated for each field and record.
3. Group related rules under one CMDT.
4. Avoid using CMDT for large transactional data.
5. Use it to replace IF-ELSE hardcoding in Apex.
Wrap-Up: Why CMDT Changes the Game
Custom Metadata Types give you the ability to:
1. Move business rules out of code.
2. Empower admins to maintain logic.
3. Deploy logic across orgs like metadata.
4. Improve maintainability and performance.
Next time you’re about to hardcode a value in Apex… stop. Ask yourself:
“Can I turn this into a Custom Metadata Type instead?”