Sub topic 6
Writing and generating a package.xml manifest
This chapter assumes you have a Salesforce DX project, an authorized org set as the default target, and have run a basic sf project retrieve start. It also assumes you can read XML. A manifest is the third way to select metadata, after a source directory and a metadata name. It is the one that scales best to large, repeatable change sets.
What a manifest is
A manifest is an XML file, conventionally named package.xml, that lists the metadata components a retrieve or deploy should act on. It holds a selection and no metadata itself. The CLI reads it, asks the org for those components (or picks them from your local source), and moves them.
A DX project keeps manifests in a manifest/ directory next to force-app/. The name package.xml is a convention. The CLI accepts any path you pass to --manifest.
Anatomy of the file
Here is the manifest from the spine of this tutorial:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>*</members>
<name>ApexClass</name>
</types>
<types>
<members>Invoice__c</members>
<name>CustomObject</name>
</types>
<version>60.0</version>
</Package>It has three kinds of elements:
<types>is one block per metadata type. Each block holds the members you want and the type they belong to.<members>names one component, or*for all components of that type. A<types>block can contain many<members>lines.<name>is the metadata type's API name, such asApexClassorCustomObject. It is case-sensitive and appears once per block, after the members.<version>is the metadata API version used for the operation.
The namespace on <Package> must be exactly http://soap.sforce.com/2006/04/metadata. A wrong namespace produces a parse error.
The version element
The <version> value sets the metadata API version for the operation. It controls which types and fields exist. A type added in a later release is invisible if the manifest says an older version, and the CLI reports it as unknown.
This is separate from sourceApiVersion in sfdx-project.json. A common rule is to keep both at the same value and raise them together when you upgrade.
Choosing members
The member text is the component's full name. How you write it depends on the type.
| Type | Member syntax | Example |
|---|---|---|
| ApexClass, ApexTrigger, LightningComponentBundle | Component name | `AccountService` |
| CustomObject (custom) | API name with `__c` | `Invoice__c` |
| CustomObject (standard) | Standard object name, listed explicitly | `Account` |
| CustomField | `Object.Field` | `Invoice__c.Amount__c` |
| Layout | `Object-Layout Name` | `Invoice__c-Invoice Layout` |
| Report, Dashboard, EmailTemplate, Document | `Folder` and `Folder/Item` | `SalesReports/Pipeline` |
Wildcards and their limits
* selects every component of a type. It is convenient, but it has edge cases:
- For
CustomObject,*returns custom objects only. Standard objects such asAccountmust be named explicitly, even if you also use*. - For folder-based types (reports, dashboards, email templates, documents), name the folder and each item as
Folder/Item. Do not rely on*to pull everything inside folders. - Some types do not support wildcards or behave differently with them. If a retrieve returns less than you expected, check the type's page in the Metadata API Developer Guide.
- In a deploy,
*is evaluated against your local source, not the org. It selects what is on disk.
Wildcards also make results depend on org contents. A * retrieve on Monday and on Friday can differ if someone added a class. For a release you want to reproduce, list members explicitly.
Mixing both styles
You can use * for one type and explicit names for another, and you can list a member twice without harm. A more targeted manifest looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>AccountService</members>
<members>InvoiceService</members>
<name>ApexClass</name>
</types>
<types>
<members>Account</members>
<members>Invoice__c</members>
<name>CustomObject</name>
</types>
<types>
<members>Invoice__c.Amount__c</members>
<name>CustomField</name>
</types>
<types>
<members>Invoice__c-Invoice Layout</members>
<name>Layout</name>
</types>
<version>60.0</version>
</Package>Retrieving CustomObject:Account brings back the object's own file, and its fields come along with it. Listing a CustomField separately is useful when you want one field without the rest of the object.
Finding the right names
If you are unsure of a type or member name, ask the org:
sf org list metadata-types --target-org my-sandbox
sf org list metadata --metadata-type ApexClass --target-org my-sandboxThe first command prints every metadata type the org's API version supports, including the type's folder and suffix information. The second lists the members of one type, which you can paste into <members> lines.
Generating a manifest instead of typing one
Hand-written manifests drift and contain typos. The CLI can generate them from three inputs: local source, a metadata list, or the org itself.
# From the local source directory
sf project generate manifest --source-dir force-app --name package --output-dir manifest
# From metadata names you specify
sf project generate manifest --metadata ApexClass --metadata CustomObject:Invoice__c --output-dir manifest
# From everything in an org
sf project generate manifest --from-org my-sandbox --output-dir manifestEach command writes a file into manifest/. Here is what each does:
--source-dirscans the directory and lists every component it finds as an explicit member, not as*. Use it to turn a branch's changes into a deploy manifest.--metadatatakes the sameTypeorType:Namesyntax assf project retrieve start. It is the quickest route to a small manifest.--from-orglists what the org contains. By default it leaves out components from installed packages. Add--include-packages managedorunlockedto include those.--namesets the file name. Without it, the file ispackage.xml.--output-dirsets the directory, defaulting to the current one.
The --type flag chooses which kind of manifest to write: package (the default), pre, post, or destroy. The last three produce the destructive changes files covered in a later chapter. They are named destructiveChangesPre.xml, destructiveChangesPost.xml, and destructiveChanges.xml.
The following diagram shows how to pick a generation route.
Using the manifest
Pass the file to a retrieve or deploy with --manifest:
sf project retrieve start --manifest manifest/package.xml --target-org my-sandboxThe next chapter covers retrieving with a manifest in detail. Deploying with one works the same way, and later chapters cover that too.
Common errors
- Wrong type name.
Apexclassis notApexClass. The retrieve warns that the type is unknown, or it returns nothing for that block. - Missing standard objects. Using
*underCustomObjectand expectingAccount. Name it explicitly. - Member not found. A typo in a member name produces a warning that the entity cannot be found. The command may still succeed for the other components, so read the whole output.
- Version too old. A type or field from a newer release is not recognized under an older
<version>. Raise the version. - Stale generated file. A manifest created from
--source-dirreflects the source at that moment. Regenerate it after you add components. - Invalid XML. A missing closing tag or a stray
&in a name stops the parse. Escape&as&.
Treat a generated manifest as a draft. Open it, check that it matches the change you intend, and commit it. That way a teammate or a CI job can repeat the exact same selection.
Exercise
You run sf project generate manifest --from-org my-sandbox --output-dir manifest. The result lists every custom object but you do not see Account. You then retrieve with that manifest. Why is Account missing, and what do you add to the manifest to include it?
Show answer
The * wildcard under CustomObject returns only custom objects, so standard objects are not included. Add a <members>Account</members> line inside the CustomObject <types> block, then retrieve again.
Exercise
Write the command that creates manifest/release.xml containing the Apex class AccountService and the custom field Invoice__c.Amount__c.
Show answer
sf project generate manifest --metadata ApexClass:AccountService --metadata CustomField:Invoice_c.Amount_c --name release --output-dir manifest