Sub topic 1
What Custom Metadata Types Are and Why Use Them
Prerequisites
This chapter assumes you already have:
- Basic Apex syntax: classes, methods, collections, and SOQL.
- Familiarity with Salesforce Setup, custom objects, and custom fields.
- A basic understanding of Apex unit tests and the
@IsTestannotation. - Some awareness of sandboxes and deployments.
The problem with hardcoded configuration
Apex often contains values that are really settings: a discount threshold, a region code, or a switch that turns a feature on. If you write them into a class, changing one means editing code, running tests, and deploying. If you store them in a normal custom object, the records do not travel with your deployment. Each org ends up with different data, and someone has to re-enter it by hand.
Custom metadata types solve both problems. They let you keep configuration outside the code while still deploying it like code.
What a custom metadata type is
A custom metadata type is a definition, similar to a custom object. It has a label, an API name that ends in __mdt, and custom fields. Its records are called custom metadata records. The type is the table layout and the records are the rows.
The key difference from a custom object is that these records are metadata, not data. This has practical effects:
- Records deploy through change sets, the Metadata API, Salesforce CLI, and packages, together with the code that uses them.
- Sandboxes get the same records as production after a deployment, so you don't copy settings by hand.
- Records are identified by a stable developer name, such as
New_Checkout, which your code can look up. - Apex can read them with
getInstance()andgetAll(), which are cheap and don't count against SOQL query limits.
A first example
Suppose you have a type named Feature_Flag__mdt with a checkbox field Is_Enabled__c. Each record is one flag, and its developer name is the flag name. This class reads one flag:
public with sharing class FeatureFlags {
public static Boolean isEnabled(String flagName) {
Feature_Flag__mdt flag = Feature_Flag__mdt.getInstance(flagName);
return flag != null && flag.Is_Enabled__c;
}
}getInstance() returns the record with the given developer name, or null if none exists. The class treats a missing flag as disabled, which is a safe default.
Callers then branch on configuration instead of a constant:
if (FeatureFlags.isEnabled('New_Checkout')) {
// new behavior
} else {
// existing behavior
}To turn the feature on in a sandbox, an admin edits the record. No Apex changes. To release it to production, you deploy the record along with the code.
When to reach for custom metadata
Custom metadata fits settings that:
- Are the same for every user and change only when you decide to change them.
- Need to move between orgs with your release.
- Are read often by code, such as feature flags, rate tables, mappings, and strategy selection.
It is a poor fit for data that users create at run time, such as orders or logs. Those belong in custom objects. The next chapter compares custom metadata with custom settings and custom labels in detail.
Exercise
Your team keeps a list of tax rates per region in a custom object. Every release, someone must re-enter the rates in each sandbox. Why would a custom metadata type help, and what would the records' developer names be used for?
Show answer
Custom metadata records are metadata, so they deploy with the code and every org gets the same rates without manual entry. The developer name, such as the region code, is a stable key that Apex can pass to getInstance() to fetch the matching record.