Sub topic 2
Metadata types and source format
This chapter assumes you have a Salesforce DX project, the sf CLI installed, and a basic idea of what Apex classes, custom objects, and Lightning components are. It explains how the CLI names metadata and how it stores that metadata on disk. You need both to select components in later chapters.
What a metadata type is
Metadata describes how an org is configured and customized. Each kind of component belongs to a metadata type, and the Metadata API gives each type a fixed name. Commands and manifests use these names.
| Component | Metadata type name |
|---|---|
| Apex class | `ApexClass` |
| Custom object | `CustomObject` |
| Custom field | `CustomField` |
| Lightning web component | `LightningComponentBundle` |
| Aura component | `AuraDefinitionBundle` |
A component is identified by its type and its full name, written Type:FullName. ApexClass:AccountService is a class. A field's full name includes its object: CustomField:Invoice__c.Amount__c.
Two formats for the same metadata
The Metadata API uses metadata format. In that format, a custom object is one large XML file that contains all of its fields, list views, and validation rules. Large files like this produce frequent merge conflicts in Git.
Salesforce DX projects use source format. The CLI breaks the large files into smaller ones, one per child component. Two people who edit different fields of the same object touch different files.
force-app/main/default/
├── classes/
│ ├── AccountService.cls
│ └── AccountService.cls-meta.xml
├── objects/
│ └── Invoice__c/
│ ├── Invoice__c.object-meta.xml
│ └── fields/
│ └── Amount__c.field-meta.xml
└── lwc/
└── invoiceList/
├── invoiceList.html
├── invoiceList.js
└── invoiceList.js-meta.xmlSome points about this layout:
- Code-based components, such as Apex classes, have a file with the code and a companion
-meta.xmlfile. The companion file holds settings such as the API version and status. - Child components of an object sit in subfolders of the object's folder. Fields go in
fields/, and list views go inlistViews/. - Bundle types such as LWC and Aura are folders. The CLI treats the whole folder as one component.
force-app/main/defaultis a package directory convention. The directories that count as source are listed insfdx-project.json.
Where the conversion happens
You do not convert files by hand during normal work. sf project retrieve start receives metadata format from the org and writes source format to your project. sf project deploy start converts your source format back before it sends the files.
If you need the Metadata API layout for another tool, convert explicitly:
sf project convert source --root-dir force-app --output-dir mdapi-outThe reverse command is sf project convert mdapi.
Common errors
- Wrong type name.
ApexorCustomFieldsis not a metadata type. The commands expectApexClassandCustomField. - Missing companion file. Deploying
AccountService.clswithout its-meta.xmlfile fails. Keep the pair together. - Incomplete field name. A field name needs its object prefix.
CustomField:Amount__cdoes not match anything.
Exercise
In source format, where does the CLI store the custom field Amount_c of the custom object Invoice_c, and what is the full metadata name you would use to select it?
Show answer
The file is force-app/main/default/objects/Invoice_c/fields/Amountc.field-meta.xml, and the name is CustomField:Invoicec.Amount_c.