Configuration-Driven Development with Custom Metadata
By the end of this lesson, you'll be able to:
- Use Custom Metadata Types to make behavior configurable without a deployment
- Query Custom Metadata Type records from Apex to drive business logic
- Recognize where the line is between genuine configuration and logic that belongs in code
Prerequisites: "State Management and Error Handling Strategy"
What Are Custom Metadata Types?
A Custom Metadata Type defines admin-editable, deployable configuration records — similar in spirit to Custom Settings, but packaged and deployed as metadata rather than data. A Discount_Rule__mdt record, for instance, could hold a threshold and a discount percentage as fields, editable directly in Setup by an admin.
Why Configuration-Driven Development?
A hardcoded discount threshold (if (orderTotal > 1000)) requires a full code change and deployment to adjust. The same threshold stored in a Custom Metadata Type record lets an admin change it directly in Setup — no code change, no deployment, no waiting on a developer for a value that's genuinely just a business parameter.
A Worked Example
public inherited sharing class DiscountService {
public static Decimal calculateDiscount(Decimal orderTotal) {
for (Discount_Rule__mdt rule : Discount_Rule__mdt.getAll().values()) {
if (orderTotal > rule.Threshold__c) {
return orderTotal * (rule.Discount_Percent__c / 100);
}
}
return 0;
}
}
Rather than hardcoded values (Lesson 1's before-example), the actual thresholds and percentages live in Discount_Rule__mdt records — an admin can add a new tier or adjust an existing one entirely through Setup.
Where the Line Is
Not everything should be configuration-driven. Genuinely complex logic — the shape of a calculation, not just its thresholds — still belongs in code. Custom Metadata Types are for values an admin should reasonably be able to adjust (thresholds, toggles, labels), not a way to avoid writing real business logic where it's actually needed.
Exercise
Explain, as a comment, whether a "maximum discount percentage allowed" value is a good candidate for a Custom Metadata Type, or whether it belongs hardcoded in Apex.
Show hint
Think about whether an admin would reasonably want to adjust this without a deployment.
Exercise
Challenge: explain, as a comment, why the actual algorithm for calculating a discount (not just its thresholds) should stay in Apex code rather than becoming configuration.
Show hint
Think about the difference between a value and a shape of logic.
Configuration-Driven Development with Custom Metadata Quiz
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
A Custom Metadata Type record lets an admin adjust a threshold or toggle without a code deployment — genuinely useful for the right kind of value, but not a substitute for real business logic.