Sub topic 3
Source format and how it maps to metadata
This chapter assumes you have generated a DX project, can read JSON, and know what Apex classes, Lightning web components, and custom objects are. You should also know that an org stores its configuration as metadata.
Two formats for the same metadata
Salesforce can describe the same org configuration in two layouts.
- Metadata format is the older layout used by the Metadata API. It groups components into one folder per type, and a package manifest (
package.xml) lists what to include. A custom object is one large.objectfile that holds all its fields, record types, and validation rules. - Source format is the layout a DX project uses. It breaks large components into smaller files, so each field, record type, and validation rule gets its own file. Smaller files produce smaller Git diffs and fewer merge conflicts.
The CLI converts between the two when you deploy or retrieve. You edit source format. The CLI sends metadata format to the org and converts what it receives back into source format.
Where metadata lands
Inside a package directory, source files sit under main/default. Each metadata type has a folder named for it.
force-app/
└── main/
└── default/
├── classes/
│ ├── AccountService.cls
│ └── AccountService.cls-meta.xml
├── lwc/
│ └── accountCard/
│ ├── accountCard.html
│ ├── accountCard.js
│ └── accountCard.js-meta.xml
└── objects/
└── Invoice__c/
├── Invoice__c.object-meta.xml
├── fields/
│ └── Amount__c.field-meta.xml
└── validationRules/
└── Amount_Positive.validationRule-meta.xmlThree patterns appear in this tree.
- Companion files. Many components have a
-meta.xmlfile next to the code. ForAccountService.cls, the companion holds the API version and status. - Bundles. A Lightning web component is a folder. The HTML, JavaScript, CSS, and its meta file travel together.
- Decomposed objects.
Invoice__cis a folder. The object's own settings live inInvoice__c.object-meta.xml. Child components such as fields sit in subfolders.
The first folder under main/ is only a convention. The CLI looks for metadata type folders anywhere under a package directory. Many teams also use folders like main/default and test/default to separate source.
A decomposed field file
Opening a field file shows why the split helps. It holds one field and nothing else.
<?xml version="1.0" encoding="UTF-8"?>
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>Amount__c</fullName>
<label>Amount</label>
<type>Currency</type>
<precision>18</precision>
<scale>2</scale>
<required>false</required>
</CustomField>Two developers who add different fields to Invoice__c now change different files. In metadata format, both would edit the same .object file and could conflict.
Converting between formats
You rarely need to convert by hand, because deploy and retrieve do it for you. When you have an older metadata-format project, the CLI can convert it.
sf project convert mdapi --root-dir mdapi-src --output-dir force-app
sf project convert source --root-dir force-app --output-dir mdapi-outThe first command turns metadata format into source format. The second does the reverse, which is useful when a tool only accepts a Metadata API package.
Common mistakes
- Putting a file in a folder that does not match its type. Move the file to the folder named for its type.
- Deleting a
-meta.xmlcompanion file. The code file then cannot deploy, because the API version and status are missing. - Editing a
package.xmlby hand in a DX project. In source format, the package directories decide what is deployed.
Exercise
A teammate adds a new field to the Invoice__c object, and you add a validation rule to the same object. Why is a merge conflict less likely in source format than in metadata format, and which files change?
Show answer
In source format each field and validation rule is its own file. The teammate adds objects/Invoice_c/fields/<Field>c.field-meta.xml, and you add objects/Invoicec/validationRules/<Rule>.validationRule-meta.xml. Different files, so Git merges them cleanly. In metadata format both changes edit the single Invoice_c.object file, which can conflict.