Advanced 18 min read

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.

APEX

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.

APEX

Configuration-Driven Development with Custom Metadata Quiz

1. What is a Custom Metadata Type used for?

2. What is the main benefit of storing a discount threshold in a Custom Metadata Type instead of hardcoding it?

3. Should the actual shape/algorithm of a complex calculation typically become configuration?

4. What kind of value is a good candidate for a Custom Metadata Type?

5. How is a Custom Metadata Type record typically queried from Apex?

Log in to submit the quiz and save your score.

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.