Sub topic 2
Metadata API format: folders, suffixes, and one file per component
This chapter assumes you know what Apex classes, custom objects, and custom fields are, can read XML, and can move around a project directory. It covers the Metadata API format: how a directory of Salesforce metadata is laid out on disk, how files are named, and how package.xml ties them together. Later chapters cover source format and conversion.
The shape of a Metadata API directory
The Metadata API is the service that retrieves and deploys configuration and code. When you retrieve with it, you get a zip file. When you deploy with it, you send one. Unzipped, the content is a plain directory. This directory layout is the Metadata API format, also called mdapi format.
Every mdapi directory follows three rules:
- A
package.xmlmanifest sits at the root. - Each metadata type has its own top-level folder, named by the API, such as
classesorobjects. - Each component is normally one file inside its type folder, named after the component with a type-specific suffix.
mdapi_output/
package.xml
classes/
MyClass.cls
MyClass.cls-meta.xml
triggers/
AccountTrigger.trigger
AccountTrigger.trigger-meta.xml
objects/
Account.object
Invoice__c.object
layouts/
Account-Account Layout.layout
staticresources/
logo.png
logo.resource-meta.xmlThe folder names are fixed by the API. You can't rename classes to apex and still deploy the directory.
Metadata types and their folders
A metadata type is a kind of configuration or code, such as ApexClass or CustomObject. A component is one instance of a type, such as the class MyClass. The type name in package.xml differs from the folder name on disk. ApexClass lives in classes. You have to know the mapping, and the Metadata API Developer Guide lists it.
| Type name | Folder | Example file | Companion file |
|---|---|---|---|
| ApexClass | `classes` | `MyClass.cls` | `MyClass.cls-meta.xml` |
| ApexTrigger | `triggers` | `AccountTrigger.trigger` | `AccountTrigger.trigger-meta.xml` |
| ApexPage | `pages` | `MyPage.page` | `MyPage.page-meta.xml` |
| CustomObject | `objects` | `Invoice__c.object` | none |
| Layout | `layouts` | `Account-Account Layout.layout` | none |
| Flow | `flows` | `My_Flow.flow` | none |
| PermissionSet | `permissionsets` | `Admin.permissionset` | none |
| StaticResource | `staticresources` | `logo.png` | `logo.resource-meta.xml` |
The suffix tells you the type of a file, and the folder tells the API where to look. Both must match. A .cls file placed in triggers is not read as an Apex class.
Some names are built from several parts. A layout is named <Object>-<Layout name>.layout. Special characters in a component name are percent-encoded in the file name. A layout called Account (Marketing) Layout becomes Account-Account %28Marketing%29 Layout.layout. If you create such files by hand, copy the encoded name from a retrieve instead of guessing.
One file per component, including large ones
In the Metadata API format, the one-file-per-component rule applies even when a component is huge. The Account.object file holds everything the API reports for the Account object:
- Custom fields.
- Record types.
- List views.
- Validation rules.
- Web links and compact layouts.
- Other child items of the object.
All of it sits inside a single CustomObject XML document. An object with hundreds of fields can produce a file with tens of thousands of lines.
<?xml version="1.0" encoding="UTF-8"?>
<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">
<fields>
<fullName>Rating_Notes__c</fullName>
<label>Rating Notes</label>
<type>Text</type>
<length>100</length>
<required>false</required>
</fields>
<fields>
<fullName>Region__c</fullName>
<label>Region</label>
<type>Text</type>
<length>40</length>
<required>false</required>
</fields>
<validationRules>
<fullName>Notes_Required_For_Hot</fullName>
<active>true</active>
<errorConditionFormula>AND(ISPICKVAL(Rating, "Hot"), ISBLANK(Rating_Notes__c))</errorConditionFormula>
<errorMessage>Add rating notes for hot accounts.</errorMessage>
</validationRules>
</CustomObject>Here, two fields and a validation rule are nested in the same file. Two developers who each add a field to Account both edit this one file. Version control then reports a conflict even though they touched different fields. This is the main reason source format exists, and a later chapter returns to it.
For a standard object like Account, the file lists only the parts you retrieved or customized, not every built-in field.
Companion -meta.xml files
Code and binary files can't hold settings such as the API version or status. The Metadata API stores those settings in a second file with the same name plus -meta.xml.
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>60.0</apiVersion>
<status>Active</status>
</ApexClass>This file is MyClass.cls-meta.xml. The .cls file holds the Apex source and nothing else. Two rules matter:
- Both files must be present and share a base name.
- If
MyClass.clsis there butMyClass.cls-meta.xmlis missing, the deploy fails.
Components that are fully described by one XML document, such as objects, layouts, and flows, need no companion file.
Folder-based types and bundles
A few types need extra structure.
Reports, dashboards, documents, and email templates live inside user-visible folders. Mdapi format models the folder as its own component:
reports/
Sales_Reports-meta.xml
Sales_Reports/
Pipeline_By_Stage.reportSales_Reports-meta.xml describes the folder, including its access type. The report goes in the subfolder. In package.xml, the member name includes the folder: Sales_Reports/Pipeline_By_Stage.
Bundle types, such as Aura components and Lightning web components, use a directory per component. Every file in it belongs to the same component:
lwc/
accountCard/
accountCard.html
accountCard.js
accountCard.css
accountCard.js-meta.xmlFor static resources, the content file keeps its own extension, such as logo.png. The companion file is named logo.resource-meta.xml and holds the content type and cache control. A later chapter treats bundles and static resources in detail.
How package.xml selects what is in the directory
package.xml is the manifest. It states which components a retrieve should return and which components a deploy should look at. It lists types and member names, and it sets the API version.
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>MyClass</members>
<name>ApexClass</name>
</types>
<types>
<members>Account</members>
<name>CustomObject</name>
</types>
<version>60.0</version>
</Package>Rules for this file:
- Each
<types>block names one metadata type in<name>and lists its components in<members>. - Member names have no suffix. Write
MyClass, notMyClass.cls. - A
*member means "all components of this type":<members>*</members>. Wildcards work for most types but not all, and the Metadata API Developer Guide lists the exceptions. Standard objects such as Account must be named explicitly. - The
<version>element sets the API version. Retrieves return the data shape of that version. - Files in the directory that the manifest does not name are ignored on deploy. Components the manifest names but the directory lacks cause an error.
On deploy, the manifest and the folders must agree. This is how the format gets its flexibility: one big directory can serve many different deploys, each with its own package.xml. It is also why a mismatch fails so often.
Retrieving and deploying in this format
The Salesforce CLI works with mdapi format through a few options. To retrieve with a manifest into a directory:
sf project retrieve start --manifest package.xml --target-metadata-dir retrieved --unzipBy default the retrieve returns a zip. --unzip extracts it into the target directory. The same command run inside a source-format project would instead write source-format files, which a later chapter covers.
To deploy a directory that is already in mdapi format:
sf project deploy start --metadata-dir mdapi_output --wait 10--metadata-dir accepts a directory or a zip file. The directory must contain package.xml at its root. If you point it at the parent folder, the API reports that the manifest is missing.
Seeing how it fits together
Common problems
- Missing companion file. A
.cls,.trigger, or.pagefile without its-meta.xmlpartner fails the deploy. Copy both files together. - Wrong folder or suffix. The API finds components by folder and suffix. A file such as
MyClass.apexis silently ignored. - Suffix in a member name.
MyClass.clsin<members>names a component that does not exist, so the deploy reports it as missing. - Manifest and folders disagree. Naming a component that is not in the directory is an error. Having files that the manifest omits is not. Those files simply are not deployed, which can hide a mistake.
- Merge conflicts in object files. Many people editing one
.objectfile collide in version control, even for unrelated fields. - Version mismatch. The
<version>inpackage.xmlcontrols the shape of retrieved data, while each class has its ownapiVersionin its companion file. They can differ, and that is allowed.
Exercise
You have a directory with package.xml at the root, classes/Billing.cls, and objects/Invoice__c.object. The manifest lists Billing under ApexClass and Invoice__c under CustomObject. A deploy of this directory fails. What is the most likely cause, and how do you fix it?
Show answer
The class has no companion file. The Metadata API needs classes/Billing.cls-meta.xml next to Billing.cls, with at least apiVersion and status. Add that file, then deploy again. The .object file needs no companion file.
Exercise
A teammate adds Region__c to Account while you add Rating_Notes__c to Account, both in Metadata API format. Why does Git report a merge conflict, and what layout avoids it?
Show answer
Both changes are edits to the same objects/Account.object file, which holds every field of the object. Git compares files by lines, so two edits near each other conflict. Source format avoids this by giving each field its own file under objects/Account/fields/.