Salesforce Custom Metadata Type Generator and Validator
When to Use This Skill
Use this skill when you need to:
- Create a Custom Metadata Type ()
- Generate or validate CMDT fields, including fields
- Generate CMDT records as deployable files
- Troubleshoot CMDT deployment errors
1. Overview and Purpose
A Custom Metadata Type produces two separate artifact families, and most requests need both:
| Artifact | Path | Metadata type |
|---|
| Type definition | objects/<Name>__mdt/<Name>__mdt.object-meta.xml
| |
| Fields | objects/<Name>__mdt/fields/<Field>__c.field-meta.xml
| |
| Records | customMetadata/<Name>.<Record>.md-meta.xml
| |
API name suffix: on the type; fields still end in
.
The defining advantage over a custom setting: CMDT records are metadata and therefore deploy between
orgs. When a user says configuration should "ship with the package" or "be the same in every org," CMDT
is the right answer.
The root element is , but almost none of a custom object's rules apply. ,
, and
are required or normal on a regular custom object and are
hard
errors here. Do not carry assumptions across from
platform-custom-object-generate
.
2. Syntactic Essentials — Type Definition (Tier 1)
Required and Allowed Elements
| Element | Requirement | Notes |
|---|
| Required | Singular UI name |
| Required | Omitting it gives Must specify a non-empty plural label for the CustomObject
|
| Always include | , or / only in dev/sandbox/scratch (Section 6) |
| Always include | What this type configures and who owns it |
being
required here but forbidden on a custom setting is the most commonly inverted
rule between the two families. Neither failure mentions the other family's rule.
Forbidden Elements
Every element below produces
Cannot specify: <element> for Custom Metadata Type
— reusing a regular custom
object's skeleton (with
,
, a
block, or
) is the
usual cause:
CORRECT — minimum valid
type:
xml
<?xml version="1.0" encoding="UTF-8"?>
<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">
<label>Partner Tier</label>
<pluralLabel>Partner Tiers</pluralLabel>
<description>Discount and threshold configuration per partner tier. Ships with the package; edited by the revenue ops team.</description>
<visibility>Public</visibility>
</CustomObject>
3. Field Rules
This skill owns the CMDT-specific deltas only — the type allowlist,
, and
below. For generic field mechanics (
derivation,
,
,
, precision/scale,
,
), follow
platform-custom-field-generate
.
Supported Field Types
,
,
,
,
,
,
,
,
,
,
,
, plus
.
Unsupported Field Types
,
,
,
,
,
,
,
,
. Each fails with a precise, well-formed error:
text
Type {TypeName} of CustomMetadataField {Object}__mdt.{Field}__c is not supported for the Entity {Object}__mdt
(There is no trailing period.) Example:
Type Currency of CustomMetadataField Partner_Tier__mdt.Discount__c is not supported for the Entity Partner_Tier__mdt
Currency is the common trap — it works on a custom setting but not here. Use
with
/
and put the currency in the label or help text.
Formula fields are unsupported, but they break the pattern above. A
element on an otherwise
legal type gives only:
is silently coerced — CRITICAL
on a
does not fail when its
resolves to a real sObject: the
deploy is green and the platform silently rewrites the field to
. A later retrieve
shows a field the user never wrote:
xml
<type>MetadataRelationship</type> <!-- was deployed as Lookup -->
Always write explicitly. Emitting
produces a green deploy and a
source-vs-org mismatch that churns in git the first time anyone retrieves. (Reproduced on a live deploy: a
resolvable
returns as
; a non-resolvable
is cleanly rejected.)
Three targets, all valid.
selects which.
| Purpose | Extra requirement |
|---|
| Another type | Link two custom metadata types | Must be a different type |
| Point at an sObject | None |
| Point at a field | Requires <metadataRelationshipControllingField>
|
xml
<?xml version="1.0" encoding="UTF-8"?>
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>Target_Field__c</fullName>
<label>Target Field</label>
<type>MetadataRelationship</type>
<referenceTo>FieldDefinition</referenceTo>
<metadataRelationshipControllingField>Partner_Tier__mdt.Target_Object__c</metadataRelationshipControllingField>
<relationshipLabel>Target Field</relationshipLabel>
<relationshipName>Target_Field</relationshipName>
</CustomField>
The controlling field must be an
relationship
on the same type, referenced as
. Omitting it gives
Metadata relationships to Field Definition require a controlling field.
Self-references are impossible. Pointing a
at its own parent type fails with
Cannot add a self-lookup relationship child with cascade or restrict options to the object itself
(verbatim
from a live
deploy) — use a second type.
Optional. It defaults to — do not add it unless the user wants a different value.
Valid values are
,
, and
.
It is valid
only on
fields. On a regular custom object field or a custom setting field:
text
Field manageability cannot be set on this entity.
4. CMDT Records ()
Filename convention
Write
customMetadata/<TypeNameWithout__mdt>.<RecordDeveloperName>.md-meta.xml
.
text
customMetadata/Partner_Tier.Bronze_AMER.md-meta.xml
The
suffix in the filename also deploys and creates a real record, so it is tolerated — but
Salesforce's retrieve normalizes to the no-suffix form, so always write it without
to avoid git
churn (detail in
references/cmdt-records.md
).
Required namespaces
All three are mandatory on the root element —
on values will not resolve without them.
xml
<CustomMetadata xmlns="http://soap.sforce.com/2006/04/metadata"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
mapping — every value needs one
| Field type | Correct | Example value |
|---|
| Checkbox | | |
| Date | | |
| DateTime | | |
| Number, Percent | | |
| Text, TextArea, LongTextArea | | |
| Email, Phone, Url | | |
| Picklist | | |
| MetadataRelationship → | | |
| MetadataRelationship → | | |
| any type, null | no — write | |
A value pointing at
another type via
was not verified — expect
with the target's DeveloperName, but confirm.
must not be emitted — CRITICAL
Salesforce's own documentation tells you to use for Picklist fields. It is wrong — it is
not a valid XML Schema type. Instead of a clean validation error, the platform fails the entire deploy
server-side with no component-level diagnostics, taking every other component down with it.
Always emit
for a Picklist field's value; never . See
references/cmdt-records.md
§2.
Record body rules
- is required. Omitting it gives
Required fields are missing: [MasterLabel]
— note it reports
the sObject field name , not .
- is optional and defaults to .
- Never put inside a block — it is a hard parse error.
- works on optional fields. On a required field it is rejected exactly as if
the field were absent. Omitting on a non-nil value is always fatal, including for text fields.
CORRECT — a complete record:
xml
<?xml version="1.0" encoding="UTF-8"?>
<CustomMetadata xmlns="http://soap.sforce.com/2006/04/metadata"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<label>Bronze AMER</label>
<protected>false</protected>
<values>
<field>Discount_Percent__c</field>
<value xsi:type="xsd:double">5.0</value>
</values>
<values>
<field>Region__c</field>
<value xsi:type="xsd:string">AMER</value>
</values>
<values>
<field>Effective_Date__c</field>
<value xsi:type="xsd:date">2024-01-15</value>
</values>
<values>
<field>Notes__c</field>
<value xsi:nil="true"/>
</values>
</CustomMetadata>
Record DeveloperName rules
The name is the second segment of the filename. It must begin with a letter, contain only alphanumerics and
underscores, not end with an underscore, not contain two consecutive underscores, and be at most 40
characters. Violations give:
text
Custom Metadata Record Name: The <Type>__mdt API Name can only contain underscores and alphanumeric characters. It must be unique, begin with a letter, not include spaces, not end with an underscore, and not contain two consecutive underscores.
Over 40 characters gives
Value too long for field: fullName maximum length is:40
(no space after the colon).
Deploy ordering
No ordering constraint — the type and every referenced field only need to
resolve (already in the org,
or present in the same deploy). Two failures tell you what is missing:
Custom metadata type <Type>__mdt is not available in this organization.
(the type is missing) and
<Type>__mdt: could not find fields: <Field>__c
(the type exists but the field does not).
→ Full record-value error catalog and the
coercion asymmetry:
references/cmdt-records.md
.
Generating records from user input
Record data arrives three ways; in each case emit
one <Type>.<Record>.md-meta.xml
per row, mapping
columns to
/
pairs with the right
:
- Inline list/table — map columns straight to fields.
- CSV — header = field API names (or map labels to them), each line = one record; take the DeveloperName
from a key column.
- Prose — infer the rows ("Bronze at 5%, Gold at 15%") and confirm the field mapping before generating.
Derive every DeveloperName
deterministically from the key column or label — never free-hand one. For each
row, run
scripts/sanitize-developer-name.sh "<LABEL>" <ROW>
before writing its record file (same input →
same valid name; empty/all-symbol/non-Latin labels fall back to
).
The transform is
many-to-one —
and
both yield
, and since
each record is
<Type>.<DeveloperName>.md-meta.xml
, a collision silently overwrites a row. After deriving the
whole batch, check duplicates
before writing; on any collision
stop and report the colliding source
labels rather than overwrite (append a deterministic
/
suffix only if the user asks to auto-resolve).
Ordered algorithm, ASCII-only limitation, uniqueness check, and worked examples:
references/cmdt-records.md
§7.
Consuming records in Apex
Records are read through the generated typed class — no SOQL, no query rows consumed, safe in loops and
triggers:
apex
Map<String, Partner_Tier__mdt> all = Partner_Tier__mdt.getAll();
Partner_Tier__mdt bronze = Partner_Tier__mdt.getInstance('Bronze_AMER'); // by DeveloperName
Accessor truncation — CRITICAL for . /
return only the
first
255 characters of any field; longer
values are silently truncated for those callers and
must be read via SOQL (
SELECT ... FROM Partner_Tier__mdt
). Say so whenever you put a
on a
CMDT.
5. Choosing the Right Component
Before generating, confirm a CMDT is what the user needs.
| If the config is… | Use | Why |
|---|
| Reference data that must deploy between orgs with its records | CMDT (this skill) | Records are metadata |
| An admin-maintained mapping / lookup / crosswalk table (field↔field, code↔code, "map A to B") | CMDT (this skill) | Editable reference data, one record per pair |
| Admin-editable per profile/user, or org-local | platform-custom-setting-generate
| Values are data and stay in one org |
| Business records users create and edit at runtime | platform-custom-object-generate
| CMDT records are not transactional data |
| Translatable UI text | Custom Label — not generated here | Labels are the translation surface |
| Credentials, API keys, tokens | Named Credential / External Credential — not generated here | See Section 6 |
| A permission check in Apex or a flow | Custom Permission — not generated here | Boolean access belongs in the permission model |
The distinction that matters most: CMDT records deploy, custom setting values do not. If the user needs
admins to edit values per profile at runtime, that is a custom setting, not a CMDT.
Field-mapping and lookup tables are a canonical CMDT use case. When a user asks to "map fields from A
to B," maintain a code-to-code lookup, or keep a crosswalk admins can edit, model it as a CMDT — one record
per pair (e.g.
/
, or
fields to
) —
never a hardcoded Apex
, constant, or Flow decision. Phrasing like "for conversion" or "for our
integration" does not make it code: if admins maintain the pairs without a deploy, it is CMDT reference data.
6. Visibility, Secrets, and the No-Silent-Downgrade Rule
and are org-type dependent
Both deploy only in a developer, sandbox, or scratch org. Anywhere else:
text
You can't set the visibility for a Custom Metadata Type to Protected unless you are in a developer, sandbox, or scratch org.
gives the same string with
substituted. The failure is confirmed in a
production-like org; that these values
succeed in a dev, sandbox, or scratch org is taken from the message
text rather than separately tested.
The no-silent-downgrade rule — CRITICAL
If fails because of the org type, never "fix" it by switching to . Stop and tell the
user. A silent downgrade turns a deliberate confidentiality choice into a world-readable component with a
green deploy and no warning — the worst possible outcome, because nothing signals that anything changed.
Report the situation instead: the org does not permit
, so the options are to deploy to a
dev/sandbox/scratch org or to accept
. Let the user choose.
This applies to any narrowing of visibility, not only this error.
Secrets do not belong in a CMDT
A CMDT is
not a secret store.
restricts access from outside the namespace, but it is not
encryption and it does not protect the value from code or admins inside it.
When a user asks to store an API key, password, token, client secret, or certificate in a CMDT:
- Warn prominently and first — before generating anything — that a CMDT is the wrong home for a
credential, and that is not encryption.
- Recommend the right component — a Named Credential with an External Credential. Say plainly
that this skill does not generate them.
- If the user still insists, comply — generate the CMDT with visibility where the org
allows it, and keep the warning in the response. Do not silently refuse, and do not silently obey.
The two rules compose, and the order matters. If the user insists on a CMDT for a secret
and the org
rejects
, the no-silent-downgrade rule is what prevents the key from landing in a
component. Stop and report — never downgrade a secret-bearing type to
.
7. Deployment Error Reference
Every type- and field-level error string is stated inline in Sections 2–4, next to the mistake that causes
it (
Cannot specify: <element> for Custom Metadata Type
, the unsupported-type string,
for a formula,
Field manageability cannot be set on this entity.
, the
controlling-
field and self-lookup errors, and the
Must specify a non-empty plural label
case).
The
full verbatim record-value catalog — every wrong-
rejection,
Required fields are missing: [MasterLabel]
, nil-on-required, the DeveloperName rules, and the exact-text traps (field named by Label not
API name, the doubled space and
typo in the no-
error,
with no
space) — is in
references/cmdt-records.md
.
One matching caution: the suffix here is
(spaced, title case); custom settings
use
. Do not assume a shared template when matching these strings.
8. Verification Checklist
Type Definition Checks
Field Checks
Record Checks CRITICAL
Consumption Checks
Security Checks CRITICAL
Component-Choice Checks
Reference File Index
| File | When to read |
|---|
references/cmdt-records.md
| Debugging a CMDT record deploy — full verbatim record-value error catalog, the type-coercion asymmetry (which mismatches the platform silently accepts), and worked record examples |