Sub topic 2
Choosing between custom metadata, custom settings, and custom labels
This chapter assumes you can write basic Apex and SOQL, know how to create custom objects and fields in Setup, and have deployed changes between a sandbox and production. By the end you should be able to pick the right storage for a piece of configuration.
The four options
Salesforce gives you several places to keep values outside your code. They look similar but behave differently.
- Custom metadata types store configuration as records. The type and its records are metadata, so they travel in deployments and packages.
- Custom settings store configuration as data. Hierarchy settings can hold different values per org, profile, or user. List settings hold named rows.
- Custom labels store single text values. They support translation.
- Custom objects store business data that users create, edit, and report on.
The key difference: metadata or data
Custom metadata records are metadata. You can deploy them with a change set, the Metadata API, or a package. A record you create in a sandbox moves to production with the rest of your release.
Custom settings records are data. Deploying the setting definition does not carry its values. You must load the values again in each org, with a data load or a script.
Custom metadata types also have some other properties:
- Reading them with
getInstance(),getAll(), or SOQL does not count against SOQL query limits in the case ofgetInstance()andgetAll(). - Apex cannot change them with DML. Updates go through the Metadata API, covered in a later chapter.
- Records can be edited in Setup without writing code.
Comparison
| Need | Custom metadata | Custom settings | Custom labels | Custom objects |
|---|---|---|---|---|
| Values deploy with the release | Yes | No, definition only | Yes | No |
| Different value per user or profile | No | Yes (hierarchy) | No | Possible with your own design |
| Apex can update values with DML | No | Yes | No | Yes |
| Translation support | No | No | Yes | Limited |
| Users create and report on records | No | No | No | Yes |
| Typed fields and validation | Yes | Yes | No, text only | Yes |
A decision flow
Worked examples
Error message shown on a page. Use a custom label. Translators can supply other languages, and you reference it in Apex as System.Label.Order_Too_Large.
Feature flag that must be the same in every org after a release. Use a custom metadata type. The flag value is deployed with the code that reads it, so the two cannot drift apart.
Per-user toggle, such as "skip validation for integration users". Use a hierarchy custom setting. Custom metadata has no per-user layer.
A counter or timestamp that your code updates on every run. Use a custom setting or custom object. Custom metadata cannot be updated with simple DML.
Discount tiers, tax strategies, or field mappings. Use a custom metadata type. These are structured, typed, and should be reviewed and deployed like code.
Common mistakes
- Choosing custom settings for release configuration, then finding production is missing values after deployment.
- Choosing custom metadata for values that code writes at run time. You end up with asynchronous deployments for a simple update.
- Putting long, translatable messages in a custom metadata text field. Labels are built for that.
- Treating custom settings and custom metadata as interchangeable because both have
getInstance(). The method names match, but the deployment and update behavior does not.
Exercise
Your team wants a checkbox that turns a new checkout flow on or off. It must have the same value in the sandbox and in production after each release, and developers want to review changes in source control. Which storage should you use, and why?
Show answer
Use a custom metadata type. Its records are metadata, so they deploy with the release and can be stored in source control. Custom settings values are data and would need a separate load in each org.
Exercise
A trigger needs to store the time of its last successful run so the next run can read it. Is custom metadata a good fit?
Show answer
No. Apex cannot update custom metadata with DML, so each write would need an asynchronous Metadata API deployment. A custom setting or custom object is a better fit for a value that code changes at run time.