Sub topic 2
Metadata types and source format
This chapter assumes you can use a terminal and know what Apex classes, custom objects, and Lightning components are. It also assumes you have a Salesforce DX project, which the previous chapter covered. Here you will learn how the CLI names metadata types, how it writes them to disk, and how those names appear in --metadata flags.
What a metadata type is
Everything you configure in an org is metadata. Each kind of component is a metadata type, and the Metadata API gives each type a fixed name. The CLI uses these names in --metadata flags and in package.xml manifests.
| Component | Metadata type name |
|---|---|
| Apex class | `ApexClass` |
| Apex trigger | `ApexTrigger` |
| Custom object | `CustomObject` |
| Custom field | `CustomField` |
| Lightning web component | `LightningComponentBundle` |
| Aura component | `AuraDefinitionBundle` |
| Page layout | `Layout` |
| Permission set | `PermissionSet` |
| Static resource | `StaticResource` |
The type name is not the same as the folder name on disk. An ApexClass lives in a folder called classes, and a LightningComponentBundle lives in lwc. When a command asks for a type, use the Metadata API name.
To see which types your org supports, list them:
sf org list metadata-types --target-org my-sandboxTo see the components of one type that exist in the org, list them:
sf org list metadata --metadata-type ApexClass --target-org my-sandboxTwo file formats
The Metadata API represents a custom object as one large XML file. That file holds the object's settings and all of its fields, list views, validation rules, and record types. Large files cause merge conflicts in Git, because two people who edit different fields still change the same file.
Salesforce DX solves this with source format. Source format splits ("decomposes") some types into many small files, so each field, list view, or validation rule gets its own file. The CLI converts between the two formats automatically:
- On retrieve, it converts metadata format into source format and writes it to your project.
- On deploy, it converts source format back into metadata format before sending it to the org.
The two formats are related like this:
You rarely handle metadata format yourself. It still matters because package.xml manifests use metadata type names and are written for the Metadata API.
What source format looks like on disk
Most files live under force-app/main/default, the default package directory from sfdx-project.json. After retrieving an Apex class and a custom object, you might see this:
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
├── listViews/
│ └── All.listView-meta.xml
└── validationRules/
└── Amount_Positive.validationRule-meta.xmlSource format follows a few patterns:
- Companion files.
AccountService.clsholds the code.AccountService.cls-meta.xmlholds settings such as the API version and status. Both must travel together. - Decomposed types.
CustomObjectbecomes a folder named after the object. The object's own settings go in*.object-meta.xml, and child components go in subfolders such asfieldsandlistViews. - The
-meta.xmlsuffix. The suffix marks files that contain metadata settings. The part before it, such as.fieldor.listView, tells you the component kind.
Here is the settings file for the class:
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>60.0</apiVersion>
<status>Active</status>
</ApexClass>Bundles
Some components consist of several files that must stay together. These are bundles, and they are stored in one folder per component.
force-app/main/default/lwc/invoiceList/
├── invoiceList.html
├── invoiceList.js
├── invoiceList.css
└── invoiceList.js-meta.xmlAura components work the same way under aura/, with one folder per component. Static resources store a .resource-meta.xml file next to the resource itself. A static resource can be a single file or an unzipped folder, and the CLI zips the folder when it deploys.
A bundle is retrieved and deployed as a unit by its folder name. LightningComponentBundle:invoiceList selects the whole folder. You cannot select just invoiceList.css.
Naming components in `--metadata`
The --metadata flag takes Type:Name. A top-level type needs only its own name. A child type needs its parent in the name:
sf project retrieve start \
--metadata ApexClass:AccountService \
--metadata CustomField:Invoice__c.Amount__c \
--metadata LightningComponentBundle:invoiceList \
--target-org my-sandboxA few rules help:
- A bare type, such as
--metadata ApexClass, selects every component of that type. - A trailing wildcard, such as
ApexClass:Account*, selects names that start with that text. Quote it so your shell does not expand it. - Layouts include the object in the name, as in
Layout:Invoice__c-Invoice Layout. Quote names that contain spaces. - Type names are case-sensitive.
Apexclassis not valid.
Converting formats explicitly
Normally you never need to convert by hand. You do need it when you work with a legacy repository, a tool that expects metadata format, or a package that was built outside DX. The CLI has two commands for this:
sf project convert source --root-dir force-app --output-dir mdapi-out
sf project convert mdapi --root-dir mdapi-out --output-dir force-appThe first command writes metadata format into mdapi-out. The second turns metadata format back into source format.
Common mistakes
- Treating the folder name as the type name.
--metadata classes:AccountServicefails. UseApexClass. - Committing only the code file. If
AccountService.clsis in a commit andAccountService.cls-meta.xmlis not, the deploy fails or uses wrong settings. Commit both. - Hand-editing a decomposed file in the wrong place. A field's definition belongs in
fields/Amount__c.field-meta.xml. Do not put it in the object's file. - Expecting one object file. Tools that search for fields inside
Invoice__c.object-meta.xmlwill find nothing. Search the wholeInvoice__cfolder. - Forgetting that some types are large.
ProfileandPermissionSetfiles can be big, and what a retrieve returns depends on which other components you retrieve alongside them. Retrieve them together with the objects and classes they refer to, or you may get partial results.
Checking your understanding
Exercise
You retrieved the custom object Invoice__c and found no fields data inside Invoice__c.object-meta.xml. Where are the fields, and what command retrieves only the field Amount__c?
Show answer
Source format decomposes the object, so each field is in its own file under objects/Invoice__c/fields/, for example Amount__c.field-meta.xml. To retrieve only that field, run sf project retrieve start --metadata CustomField:Invoice__c.Amount__c.
Exercise
A teammate's deploy of a new Apex class fails after they committed only Report.cls. What file is missing, and why does it matter?
Show answer
The companion file Report.cls-meta.xml is missing. It holds the API version and status. The deploy needs it to know how to treat the class, so both files must be committed and deployed together.