Sub topic 3
Defining a Type and Its Fields
This chapter assumes you can write basic Apex, navigate Salesforce Setup, and create custom objects and fields. It shows how to define a custom metadata type and choose its fields. Creating the records comes in the next chapter.
What you are creating
A custom metadata type is a template, like a custom object definition. Each record of the type holds one piece of configuration. The type is itself metadata, so you can deploy it between orgs with change sets, the Salesforce CLI, or packages.
Setup adds two suffixes to API names:
- The type gets the suffix
__mdt, for exampleFeature_Flag__mdt. - Each field gets the suffix
__c, for exampleIs_Enabled__c.
In this chapter we define a type named Feature_Flag__mdt with one checkbox field. Later chapters read it in Apex.
Creating the type in Setup
- Open Setup and search for Custom Metadata Types.
- Click New Custom Metadata Type.
- Enter a Label such as
Feature Flagand a Plural Label such asFeature Flags. - Check that Object Name is
Feature_Flag. Salesforce derives it from the label, and it becomesFeature_Flag__mdtin Apex. - Choose a Visibility setting. Use Public for most work. The visibility chapter explains the other choices.
- Save.
Every record automatically gets two standard fields. Label is the display name. Label Name (the DeveloperName field) is the unique API name. Apex looks records up by DeveloperName, so choose names that are stable and meaningful.
Adding fields
On the type's detail page, open Custom Fields and click New. Pick a data type, name the field, and set its options.
Custom metadata types support a smaller set of field types than custom objects:
| Field type | Typical use |
|---|---|
| Checkbox | Flags such as `Is_Enabled__c` |
| Text | Names, codes, and Apex class names |
| Number | Thresholds, percentages, limits |
| Picklist | A fixed set of allowed values |
| Date, Date/Time | Effective dates |
| Email, Phone, URL | Contact or endpoint values |
| Metadata Relationship | A link to another type or object (a later chapter) |
Formula, roll-up summary, and auto-number fields are not available. Text Area (Long) is limited compared with a custom object, so keep values short.
Add the flag field with these settings:
- Field Type: Checkbox
- Field Label:
Is Enabled - Field Name:
Is_Enabled__c - Default Value: Unchecked
Field manageability
Each field has a Field Manageability setting. It matters once you package the type.
- Locked after release: the package developer can change or delete the field in later versions, but subscribers cannot change it.
- Subscriber editable: the subscriber can change the field's value on package records.
In an unpackaged org this setting has little effect. Set it deliberately anyway, because changing it later can be restricted.
Designing fields well
Treat the fields as the schema of your configuration.
- Use a Picklist instead of Text when only a few values are valid. This stops typos in records.
- Store an Apex class name in a Text field only when you plan to load it dynamically.
- Add an
Is_Active__ccheckbox to types where you want to retire records without deleting them. - Pick numeric precision with care. Percentages and money need decimal places.
flowchart TD
A[Create type Feature_Flag__mdt] --> B[Add field Is_Enabled__c]
B --> C[Create records]
C --> D[Read in Apex]The diagram above is only a sketch of the order of work. The next two chapters follow the same order.
What the type looks like in code
Once defined, Apex treats the type like an object. You reference the type and fields by API name:
Feature_Flag__mdt flag = Feature_Flag__mdt.getInstance('New_Checkout');
Boolean enabled = flag != null && flag.Is_Enabled__c;If you misspell a field name, the code fails to compile. This is an advantage over hardcoded strings.
Exercise
You want a custom metadata type to hold tax settings per region. Each record must store a region code, a tax percentage, and one of three allowed rounding modes. Which field types do you choose, and which standard field holds the region code if you want to look records up by it?
Show answer
Use a Number field with decimal places for the percentage and a Picklist with the three rounding modes. The region code can be the record's Label Name (DeveloperName), because Apex looks records up by that value. If the code contains characters that DeveloperName does not allow, use a separate Text field instead.