Sub topic 1
What Custom Metadata Types Are and Why Use Them
Prerequisites
Assume you know basic Apex classes, methods, collections, and SOQL. You should also understand Salesforce Setup, custom objects, and Apex tests.
Custom metadata types
A custom metadata type (CMT) defines configuration records that Salesforce treats as metadata. The records describe how an application should behave, such as whether a feature is enabled or which discount tier applies.
CMT records are deployable and packageable. They can move from a sandbox to production with metadata deployments. Apex can read them without hardcoding every configuration value.
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;
}
}If New_Checkout is a record in Feature_Flag__mdt, changing its Is_Enabled__c value changes the behavior without changing Apex code.
Choosing a configuration mechanism
| Mechanism | Best use | Deployment behavior |
|---|---|---|
| Custom metadata type | Structured, deployable application configuration | Records move with metadata |
| Custom setting | Runtime or org and user-specific settings | Usually treated as data |
| Custom label | Translatable text shown to users | Label metadata is deployable |
| Custom object | Business data and transactional records | Records need a data-loading process |
Use CMT when configuration has fields, relationships, or rules. Use a custom label for a short message, not for a collection of related settings. Use a custom object when users must create and edit business records regularly.
Defining a type and its fields
In Setup, open Custom Metadata Types and create a type such as Feature Flag. Add fields such as:
Is Enabled, a Checkbox fieldDescription, a Text fieldMinimum Version, a Text field
Salesforce gives each type an API name, such as Feature_Flag__mdt. Each record also has a developer name. Apex uses that stable developer name when calling getInstance().
Creating and managing records
Create records from the type’s Manage Records page in Setup. Give each record a developer name such as New_Checkout, then fill in its fields. Include the type and records in a metadata deployment or package.
CMT records do not support ordinary Apex insert or update DML. Treat configuration changes as deployment operations. If administrators need an application UI for changes, use the Metadata API rather than pretending these records are normal business data.
Exercise
Which mechanism should hold a deployable feature flag with an enabled checkbox?
Show answer
A custom metadata type, because it stores structured configuration that can move between orgs with metadata.