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 only. Source format and conversion come in later chapters.
What the format is
Metadata API format is the layout Salesforce uses when you retrieve or deploy metadata through the Metadata API. A retrieve returns a zip file, and a deploy takes a zip file. Inside, the files follow a fixed structure:
- A
package.xmlmanifest at the root. - One folder per metadata type, such as
classesorobjects. - One file per component inside each folder.
A component is one instance of a metadata type. MyClass is a component of the type ApexClass. Account is a component of the type CustomObject.
Folders and suffixes
Each metadata type has a folder name and a file suffix. The folder name usually differs from the type name, so you need to know both. These are defined by Salesforce, and you can't rename them.
| Metadata type | Folder | File suffix |
|---|---|---|
| `ApexClass` | `classes` | `.cls` |
| `ApexTrigger` | `triggers` | `.trigger` |
| `CustomObject` | `objects` | `.object` |
| `Layout` | `layouts` | `.layout` |
Some types need two files. An Apex class has its code in MyClass.cls and its settings, such as the API version and status, in MyClass.cls-meta.xml. The -meta.xml file is a companion file. Later chapters cover it in detail.
One file per component
In this format, a custom object is one file. objects/Account.object holds the object's settings, and it also holds every custom field, list view, validation rule, and record type defined on that object. That is why the file is called monolithic. A large org can produce object files with tens of thousands of lines.
package.xml
classes/
MyClass.cls
MyClass.cls-meta.xml
objects/
Account.objectIf two developers each add a field to Account, both edit the same file. Version control then reports a conflict in one big XML document, even though the two changes are unrelated. This is the main reason source format exists.
The manifest ties it together
The package.xml file lists what the zip contains. Each <types> block names one metadata type and lists its members. The <version> element sets the API version for the operation.
<?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>Here <name> holds the metadata type, not the folder. The manifest says ApexClass, while the folder is classes. A frequent mistake is writing the folder name in <name>, which fails the deploy.
The manifest controls which components are processed. A file in the classes folder that isn't listed in package.xml is not deployed. A member listed in the manifest with no matching file causes an error.
How the pieces relate
Common errors
- Wrong name in the manifest. Using
classesinstead ofApexClassin<name>. - Missing companion file. Adding
MyClass.clswithoutMyClass.cls-meta.xml. The deploy fails. - Manifest and folders out of step. A component exists in a folder but isn't listed, so it is silently skipped.
- Merge conflicts in object files. Several people change one
.objectfile at the same time.
Exercise
A deploy zip contains classes/Helper.cls and classes/Helper.cls-meta.xml. The package.xml lists only <members>Account</members> under CustomObject. Is Helper deployed, and what should you change if you want it deployed?
Show answer
No. The manifest controls what is processed, and Helper is not listed. Add a <types> block with <members>Helper</members> and <name>ApexClass</name>.
Exercise
Why do two developers who add different fields to the same object often get a merge conflict in Metadata API format?
Show answer
The format keeps the whole custom object, including all its fields, in one Account.object file. Both developers edit the same file, so version control sees overlapping changes in one document.