Sub topic 2
Anatomy of package.xml
This chapter assumes you can read basic XML and know common metadata types such as Apex classes and custom fields. It also assumes you have a Salesforce DX project with a force-app directory. You don't need to run any commands yet.
What the file does
A package.xml file is a manifest. It lists the metadata components that a retrieve or deploy should act on. The Metadata API reads the manifest and works only on the components it names. A component missing from the manifest is left alone.
The same format is used later for destructive changes files, so the structure here carries over.
The four building blocks
Here is a small manifest that names two Apex classes and one custom field.
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>AccountService</members>
<members>AccountServiceTest</members>
<name>ApexClass</name>
</types>
<types>
<members>Account.Region__c</members>
<name>CustomField</name>
</types>
<version>62.0</version>
</Package>The file has these parts:
<Package>is the root element. Itsxmlnsattribute must behttp://soap.sforce.com/2006/04/metadata. Without the namespace, the Metadata API does not recognize the file.<types>is a group. Each group covers exactly one metadata type. You repeat the element once per type.<name>sits inside<types>and holds the metadata type name, such asApexClassorCustomField. Each<types>block has one<name>.<members>sits inside<types>and holds the name of one component. You repeat it once per component.<version>is a single API version number, placed once at the end of<Package>.
Reading the example
The first <types> block says "retrieve or deploy these Apex classes." The <name> value, ApexClass, is the type. The two <members> values are class names without a file extension. You write AccountService, not AccountService.cls.
The second block names one custom field. A field is not a standalone component. It belongs to an object, so its member name is Object.FieldName. The next chapter covers naming rules in detail. For now, remember that the member string must match the component's full name in the org.
What the version does
The <version> value is a Metadata API version. It decides which metadata types and which fields of each type the request understands. If you name a type that your version does not support, the request fails. A newer version can also return extra fields for a component than an older one.
Use the same version your project targets, so retrieved files match what your tooling expects. The sourceApiVersion setting in sfdx-project.json is a separate value that belongs to the project, so check both when results look unexpected.
Rules of thumb
- Group all members of one type under one
<types>block. Splitting a type across several blocks is easier to misread, so avoid it. - Keep type blocks in alphabetical order, with members sorted inside each block. Order does not change behavior, but sorted manifests make version control diffs readable.
- Metadata type names are case-sensitive.
Apexclassis notApexClass. - Always include
<version>in a retrieve or deploy manifest. - A
<types>block with a<name>but no<members>selects nothing, so it has no effect.
Common errors
| Symptom | Likely cause |
|---|---|
| The file is rejected before any work starts | The `xmlns` namespace is missing or misspelled, or the XML is malformed |
| Error about an unknown or unsupported type | The `<name>` has a typo, or the `<version>` is too old for that type |
| Nothing is retrieved for a member | The member name does not match the org, for example a missing `__c` suffix |
| Wrong component retrieved | The member is under the wrong `<name>`, such as a field listed under `CustomObject` |
When a result surprises you, read the file from the top. Check the namespace, then each <name>, then each <members> string, and finally the version. Most manifest problems come from one of these.
Exercise
A teammate writes a manifest with a <types> block that has <name>Account.Region__c</name> and <members>CustomField</members>. What is wrong, and how do you fix it?
Show answer
The two values are swapped. <name> must hold the metadata type, and <members> must hold the component name. Change it to <members>Account.Region__c</members> and <name>CustomField</name>.
Exercise
Write a minimal manifest that selects the Apex class OrderService using API version 62.0.
Show answer
Use the Package root with the metadata namespace, one types block with <members>OrderService</members> and <name>ApexClass</name>, and <version>62.0</version> after it.