Sub topic 2
Anatomy of package.xml
This chapter assumes you can read basic XML and have a Salesforce DX project with a force-app directory. It also assumes the Salesforce CLI (sf) is installed and authorized to a sandbox or scratch org. You should know what Apex classes, custom objects, and custom fields are, but you don't need to have written a manifest before.
Why a manifest exists
The Metadata API moves configuration and code between an org and your local files. It works with named metadata components, such as the Apex class AccountService or the custom field Account.Region__c. Each component belongs to a metadata type, such as ApexClass or CustomField.
When you retrieve or deploy, you must say which components you mean. A manifest is the XML file that says it. By convention it is called package.xml and lives in a manifest folder in a DX project.
The same format appears in three other places:
- The CLI can generate a manifest from your source files.
- A destructive changes file uses the same layout to list components to delete.
- A retrieve command can use a manifest to select what to pull from an org.
Learning the structure once covers all of them.
A complete example
<?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>This manifest selects two Apex classes and one custom field. It contains four kinds of element, described below.
The elements one by one
| Element | Where it appears | Count | Meaning |
|---|---|---|---|
| `Package` | Root | Exactly one | Wrapper that carries the metadata namespace |
| `types` | Inside `Package` | One per metadata type | Groups the components of a single type |
| `members` | Inside `types` | One per component | The name of one component, or `*` |
| `name` | Inside `types` | Exactly one per `types` | The metadata type, such as `ApexClass` |
| `version` | Inside `Package` | Exactly one | The Metadata API version, such as `62.0` |
The Package root and its namespace
The root element is Package, and it must declare the namespace http://soap.sforce.com/2006/04/metadata. This is an XML namespace identifier. It does not point to a page you need to reach, and it must be copied exactly. If you misspell it or leave it out, the Metadata API can't recognize the file as a manifest. The error you see depends on the command and the API version.
A related point: Package here is the root of an XML manifest. It is unrelated to managed packages or second-generation packages, although a manifest can be used to retrieve a package's contents.
types, name, and members
Each types block describes one metadata type. Its name child holds the type's API name, and one or more members children list the components of that type.
Three things to keep in mind:
- The
namevalue is a Metadata API type name, not the label you see in Setup. UseApexClass,CustomObject,CustomField,Flow,PermissionSet,LightningComponentBundle, and so on. Type names are case-sensitive. - Each type should appear in only one
typesblock. Splitting a type across two blocks is a common result of merging two manifests by hand. Merge the members into one block instead. - Every
membersvalue is the component's full name as the Metadata API sees it. That is not always the file name on disk. Fields useObject.Field, and folder-based types useFolder/Name. The next chapter covers these rules in detail.
By convention, members come before name inside a types block, as in the example above. Tools that write manifests follow this order. It's easiest to do the same.
The version element
The version element sets the Metadata API version used for the operation. It has a major and a minor part, and the minor part is always 0, as in 62.0. Each Salesforce release adds one to the major number.
The version affects which metadata types and fields the API understands. A manifest that names a type introduced in API 62.0 fails if you set version to 45.0. A version higher than the org supports also fails. For a deploy, check the org's current API version, then set the manifest to that version or lower.
The version is not the same as sourceApiVersion in sfdx-project.json. That setting tells the CLI which API version to use when it converts your source files to Metadata API format. The manifest's version governs the manifest-driven operation. Keeping the two in step avoids confusing differences, but they are separate settings.
The wildcard member
A members value of * selects every component of that type:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>*</members>
<name>ApexClass</name>
</types>
<version>62.0</version>
</Package>This works in retrieve and deploy manifests. A wildcard does not work in a destructive changes file, because deletions must name each component explicitly. Some types also behave differently with wildcards. Standard objects and folder-based types are the common examples, and the next chapter covers those. Don't assume a wildcard gives you everything.
You can mix a wildcard with named members of the same type, but this is redundant. The wildcard already covers them, so keep one or the other.
Reading a manifest programmatically
A manifest is plain XML, so you can check it before you hand it to the CLI. The script below prints each type with its members and flags the most common structural errors: a wrong root element, a missing version, and a type that is repeated.
import sys
import xml.etree.ElementTree as ET
NS_URI = "http://soap.sforce.com/2006/04/metadata"
NS = {"m": NS_URI}
def summarize(path):
root = ET.parse(path).getroot()
if root.tag != "{%s}Package" % NS_URI:
raise ValueError("Root must be <Package> in namespace " + NS_URI)
seen = set()
for block in root.findall("m:types", NS):
type_name = block.findtext("m:name", namespaces=NS)
members = [m.text for m in block.findall("m:members", NS)]
if not type_name:
raise ValueError("A <types> block has no <name>")
if type_name in seen:
print("WARNING: type listed more than once: " + type_name)
seen.add(type_name)
print("%s: %s" % (type_name, ", ".join(members) or "(no members)"))
version = root.findtext("m:version", namespaces=NS)
if version is None:
raise ValueError("Missing <version>")
print("API version: " + version)
if __name__ == "__main__":
summarize(sys.argv[1] if len(sys.argv) > 1 else "manifest/package.xml")Run it with python check_manifest.py manifest/package.xml. For the example above, the output is:
ApexClass: AccountService, AccountServiceTest
CustomField: Account.Region__c
API version: 62.0The script checks structure only. It can't tell you whether AccountService exists in the org. Only a retrieve or a deploy tests that.
An empty manifest
A manifest with no types at all is valid, and it has a specific use. A deployment must always include a manifest. If you only want to delete components, you pair a destructive changes file with a manifest like this one:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<version>62.0</version>
</Package>It deploys nothing and gives the deletion a valid manifest to travel with. The deletion chapters return to this.
Where a manifest comes from
You have two ways to produce a manifest: write it by hand, or generate it. Hand-writing suits small, targeted changes, such as a release that touches five components. Generating suits larger sets. The CLI can build a manifest from the source in your project:
sf project generate manifest \
--source-dir force-app \
--name manifest/package.xmlThis command scans force-app and writes a manifest that lists every component it finds. The output is a starting point. Open it and review it before you rely on it. A later chapter covers generation options.
Common errors
The following mistakes cause most manifest failures.
- Wrong or missing namespace. The
xmlnsattribute onPackagehas a typo, or it is missing. Copy it from a known good file. - Misspelled type name.
ApexClasses,Apexclass, orCustom Fieldis not a type name. Use the exact API name, such asApexClassorCustomField. - File name used instead of member name. The file
Region__c.field-meta.xmlbecomes the memberAccount.Region__c, notRegion__c. For a custom field, use the formObject.Field. - Missing
version. The manifest has noversion, or the number has the wrong format, such as62instead of62.0. - Version too old or too new. The manifest names a component the chosen API version doesn't know, or it asks for a version the org doesn't support.
- Type repeated across blocks. The same
nameappears in twotypesblocks. Combine them. - Malformed XML. An unclosed tag, an unescaped
&in a member name, or a stray character before the XML declaration. A text editor with XML validation catches these quickly. - Components that don't exist. For a retrieve, a named member that isn't in the org usually produces a warning that the entity was not found. A deploy that names a file missing from the source fails. Check spelling and, for a deploy, check that the file is in the project.
What to remember
A manifest is a Package root element in the Metadata API namespace. Inside it, each types block pairs one name (the metadata type) with one or more members (component names). A single version element sets the API version. Most failures come from the namespace, the type name, the member name, or the version. Destructive changes files reuse this format, so each of these checks applies to them too.
Exercise
A teammate sends you this manifest and a retrieve against it returns nothing for the custom field. Find the two problems. <?xml version="1.0" encoding="UTF-8"?><Package xmlns="http://soap.sforce.com/2006/04/metadata"><types><members>Region__c</members><name>CustomFields</name></types></Package>
Show answer
First, the type name is wrong. It must be the exact API name CustomField, not CustomFields. Second, the member must use the Object.Field form, such as Account.Region_c, not just Region_c. A third defect is that the manifest has no version element, which a retrieve or deploy needs, so add <version>62.0</version> or the version your org supports.
Exercise
You want to delete one Apex class and nothing else. What manifest do you pair with the destructive changes file, and why is it needed?
Show answer
Use a package.xml with the Package root, the metadata namespace, and a version element, but no types. A deployment requires a manifest, and this one deploys no components, so only the deletions in the destructive changes file take effect.