Sub topic 2
Choosing between custom metadata, custom settings, and custom labels
This chapter assumes you know basic Apex, can find your way around Setup, and understand that Salesforce orgs are moved between environments by deployment. It does not cover how to build a custom metadata type, which comes in the next chapters. The goal here is to pick the right place to store a piece of configuration before you write code that depends on it.
The core difference: metadata or data
Salesforce offers four common places to keep a value outside your Apex code. Two properties separate them:
- Is the value metadata or data? Metadata moves between orgs with a deployment (change set, Salesforce CLI, or package). Data stays in the org where someone entered it.
- Who changes it, and how? Some values are changed by admins in Setup, some by Apex code at runtime, and some by end users.
Custom metadata type records and custom labels are metadata. Custom setting records and custom object records are data.
| Option | What the value is | Travels in a deployment | Changed from Apex | Typical use |
|---|---|---|---|---|
| Custom metadata type | Records of a type you define, with typed fields | Yes, the records deploy | Only through the Metadata API (asynchronous) | Rules, feature flags, mappings, strategy names |
| Custom setting (list) | Data records with typed fields | Definition only, not the records | Yes, with DML | Org-level lookup tables that change at runtime |
| Custom setting (hierarchy) | Values resolved per user, profile, or org | Definition only, not the values | Yes, with DML | Per-user or per-profile switches and limits |
| Custom label | One text value, translatable | Yes | No | User-facing text and messages |
| Custom object | Ordinary data records | Definition only | Yes, with DML | Business data, large or user-edited sets |
Custom labels: text people read
A custom label is a single text value of up to 1,000 characters. You can translate it with the Translation Workbench, and you reference it from Apex, Visualforce, Aura, and Lightning Web Components. Use a label when the value is wording shown to a user, especially if it must appear in several languages.
String greeting = System.Label.Welcome_Message;Labels are a poor fit for logic. A label holds one string, so it cannot carry a number, a checkbox, or several related fields. If you find yourself storing 'true' in a label and comparing it to a string, you want a different tool.
Custom settings: data with a fast cache
A custom setting looks like a small custom object, and the platform caches its values in the application cache so reads are cheap. There are two kinds:
- List custom settings store a set of named records, readable with
getAll()andgetInstance(name). - Hierarchy custom settings resolve a value by walking from the running user, to the user's profile, to the org default. This is the only built-in option that gives different users different values without any code to select among them.
Checkout_Setting__c setting = Checkout_Setting__c.getInstance();
Integer maxItems = setting.Max_Items__c == null ? 10 : Integer.valueOf(setting.Max_Items__c);getInstance() with no argument returns the value that applies to the current user. If nothing is defined, it returns an empty record rather than null, so check for empty fields, not for null.
The records are data. When you deploy a custom setting, the definition goes to the target org but the values do not. Someone must re-enter them in each sandbox and in production, or you must load them with a script. That is the main reason teams moved configuration to custom metadata. Settings also have a strength that metadata lacks: Apex can change them with ordinary DML, so a setting suits a value your code updates, such as a "last run" marker or a per-user preference.
Custom metadata types: configuration that ships with the code
A custom metadata type is a definition you create, and each record of that type is itself metadata. The records are included in change sets, source-tracked projects, and packages, so a feature flag set in a sandbox can be deployed to production together with the Apex that reads it. The deployed record and the code that depends on it arrive as one unit, and there is no manual step after the deployment.
Feature_Flag__mdt flag = Feature_Flag__mdt.getInstance('New_Checkout');
Boolean enabled = flag != null && flag.Is_Enabled__c;Other properties worth knowing when you compare:
- Records are read with
getInstance(),getAll(), or SOQL. The next chapters cover each. - Apex cannot insert or update the records with DML. Changing a value at runtime means an asynchronous Metadata API deployment, which is slow and heavy compared with an update to a setting.
- Records are normally edited by admins or developers, not by end users.
- A type can have relationships to other metadata, and records can reference an Apex class by name.
Custom objects: when it is really data
A custom object is the right choice when the records are business data. This includes many records, records that users create and edit, records you need to report on, or records that relate to other objects, have sharing rules, or need triggers and automation. Reading them costs SOQL queries and writing costs DML. That is expected for data, and it is a reason not to use an object for configuration that your code reads on every request. Configuration stored in an object also leaves you with no deployment story for the records, and your tests must create them before every run.
A decision path
Ask the questions in this order and stop at the first clear answer.
Some cases the path resolves in a way that surprises people:
- A feature flag that differs per user is a hierarchy custom setting, even though flags usually belong in custom metadata. The deployment benefit loses to the per-user need. A flag that is the same for everyone belongs in custom metadata.
- A tax rate table that changes only when finance approves a release is custom metadata. The same table edited by a business user every week is a custom object.
- An error message shown on a page is a custom label, even if the logic that picks it is driven by custom metadata. Store the label name in a metadata field, not the sentence.
Reading the three side by side
This class shows how each source is read. It assumes the label, the hierarchy setting, and the metadata type already exist in the org.
public with sharing class ConfigReaders {
public static String welcomeText() {
return System.Label.Welcome_Message;
}
public static Boolean isFlagOn(String flagName) {
Feature_Flag__mdt flag = Feature_Flag__mdt.getInstance(flagName);
return flag != null && flag.Is_Enabled__c;
}
public static void rememberLastRun() {
Checkout_Setting__c orgDefaults = Checkout_Setting__c.getOrgDefaults();
orgDefaults.Last_Run__c = System.now();
upsert orgDefaults;
}
}The last method is the one operation only a custom setting supports directly: it writes with upsert. Trying the same on Feature_Flag__mdt would fail, because Apex has no DML for metadata records.
Common mistakes
- Using a custom setting for configuration that must exist in every environment. The code deploys, the values do not, and production behaves differently from the sandbox where you tested. Move such values to custom metadata.
- Using custom metadata for state your code updates. A counter or timestamp needs a custom setting or custom object, because a Metadata API deployment is asynchronous and not designed for frequent writes.
- Putting logic switches in labels. Labels carry text. A value read as a string and compared to
'true'breaks silently when someone edits the wording or a translation. - Storing configuration in a custom object and querying it on every call. This spends SOQL queries and requires test data setup for something that is not business data.
- Expecting an empty hierarchy setting to be null.
getInstance()returns an empty record, so a null check never fires and the default branch never runs.
Summary of the guidelines
- Text people read: custom label.
- Configuration that should travel with the code: custom metadata type.
- Values that differ by user, or that Apex must change at runtime: custom setting.
- Records people create and maintain as business data: custom object.
You can mix them. A strategy name stored in custom metadata can point to a label for its message, and a hierarchy setting can override a metadata default for a single user, as long as your code defines which one wins.
Exercise
Your team keeps a "maximum discount percent" in a hierarchy custom setting. It works in the sandbox, but after the first production deployment, orders get no discount cap at all. Why does this happen, and which storage type fixes it if the cap is the same for every user?
Show answer
Deploying a custom setting moves its definition but not its records, so production has the field but no values. Code that reads the empty record sees blank fields and applies no cap. If the cap is the same for all users and should ship with the code, move it to a custom metadata type, whose records deploy with the Apex that reads them.
Exercise
A page shows the message "Your cart is empty" in English, French, and German. Where should the sentence live, and where should the code that decides when to show it get its rule from if the rule is a toggle that must deploy with the code?
Show answer
Store the sentence in a custom label, so the Translation Workbench can supply the French and German versions. Keep the toggle in a custom metadata record, which deploys with the code. Do not store the sentence in the metadata record, because it would not be translated.