Sub topic 7
Retrieving with a Manifest
A manifest lets you describe a retrieve in a file instead of on the command line. Once the list of components grows past two or three, the file is easier to review, share, and reuse. This chapter covers how the command reads a manifest, where the files end up, and which errors to expect.
Prerequisites
This chapter assumes you have:
- A Salesforce DX project with a valid
sfdx-project.json. - An authorized org, ideally set as your default with
sf config set target-org. - A
package.xmlfile, or the knowledge from the previous chapter to write or generate one. - Familiarity with the
--metadataform ofsf project retrieve start.
Why use a manifest
The --metadata flag works well for a few named components. It becomes hard to manage when you need many components, whole types, or the same selection every time. A manifest has three advantages:
- It lives in version control, so a teammate can retrieve exactly the same set.
- It supports the
*wildcard, which selects every component of a type. - It is the same format Metadata API tools and CI jobs already use.
The manifest used in this chapter
This manifest selects every Apex class and one custom object.
<?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>Each <types> block holds one metadata type in <name> and one or more <members>. A member is either a component's full name or *. The <version> element is the Metadata API version used for the request.
Save the file as manifest/package.xml in your project root. The manifest folder is the convention that sf project generate manifest also uses.
Running the retrieve
sf project retrieve start --manifest manifest/package.xml --target-org my-sandboxThe command does the following:
- It reads the manifest and sends a retrieve request for those components to the org.
- It waits for the org to build the result. The default wait is 33 minutes, and you can change it with
--wait. - It converts the returned metadata to source format.
- It writes the files into your project's package directories.
- It prints a table of the files that were created or changed.
You can shorten the command by omitting --target-org when a default target org is set. The short flags are -x for --manifest and -o for --target-org.
<br>
Where the files land
The CLI decides each file's location in two ways:
- A component that already exists locally is overwritten in place, wherever it lives in your project.
- A component that is new to the project goes into the default package directory. That is the entry in
sfdx-project.jsonwith"default": true.
If your project has several package directories, new components do not go to the one you might expect. Check the printed table, or run git status, after the first retrieve.
Retrieve does not delete local files. If someone removes a class in the org, the local .cls file stays after you retrieve. Removing components is covered in the chapter on destructive changes.
Wildcards and what they really select
* selects every component of that type in the org. For ApexClass, that includes every class in the org that can be retrieved. Managed package components are not included, because the subscriber org cannot retrieve their code.
Wildcards have three limits worth knowing:
- Folder-based types such as
Report,Dashboard,Document, andEmailTemplatedo not return folder contents with*. Name the folder and each item, for exampleMyReports/Weekly_Sales. - A wildcard on a large org can produce a large result. If the retrieve times out or the file count surprises you, narrow the manifest to named members.
- Some types, such as
ProfileandPermissionSet, return only the parts that relate to the other components in the same request. A profile retrieved alone is often nearly empty. Retrieve it together with the objects and classes it governs.
You can mix a wildcard for one type with named members for another, as the example above does. You can also list several named members under one type:
<types>
<members>AccountService</members>
<members>InvoiceService</members>
<name>ApexClass</name>
</types>Parent and child components
Some metadata types contain others. Retrieving CustomObject for Invoice__c also returns its fields, list views, validation rules, and similar child items. In source format these are decomposed into separate files under objects/Invoice__c/. You do not list the fields separately unless you want only the fields and not the object file.
The reverse also works. Listing CustomField with a member such as Invoice__c.Amount__c retrieves only that field. The member name uses the form ParentObject.ChildName.
Generating the manifest instead of writing it
Writing XML by hand invites typos. You can generate a starting file from a type name or from the source you already have:
sf project generate manifest --metadata ApexClass --metadata CustomObject:Invoice__c --output-dir manifestThe command writes manifest/package.xml. Open it and review the result before you commit it, and edit it by hand to add anything the generator did not include.
Combining a manifest with other flags
These flags are the ones you will use most often with a manifest:
| Flag | Purpose |
|---|---|
| `--target-org`, `-o` | The org to retrieve from. Optional when a default target org is set. |
| `--wait`, `-w` | Minutes to wait before the command returns control. |
| `--api-version` | Overrides the API version used for the request. |
| `--ignore-conflicts`, `-c` | Overwrite local files even if tracking reports a conflict. |
| `--output-dir`, `-t` | Retrieve into a separate directory in metadata format instead of your project. |
Use one selector per command. A single retrieve start should use --manifest, --metadata, or --source-dir, not a mixture. If you want a manifest and one extra component, add the component to the manifest.
<br>
Common errors with manifest retrieves
| Message or symptom | Likely cause | Fix |
|---|---|---|
| A warning that an entity of a type named X cannot be found | A member name is misspelled, or the component does not exist in this org | Check the name and the org with `--target-org` |
| An error about an invalid or unknown type name | A typo in `<name>`, such as `ApexClasses` | Use the exact Metadata API type name |
| The command finds no manifest file | A wrong relative path | Run from the project root, or pass the full path |
| A parse error mentioning XML | Missing closing tag or unescaped characters | Validate the XML in an editor |
| Retrieved fewer files than expected | A `.forceignore` rule excluded them, or a wildcard limit applied | Check `.forceignore` and the type's wildcard behavior |
| Files changed that you did not expect | A wildcard pulled in components that others edited in the org | Review with `git diff` before committing |
A retrieve with a bad member name often completes with a warning rather than a failure. Read the output, not just the exit code, when you script it.
A practical workflow
- Generate a manifest, or copy one from a teammate.
- Trim it to the components relevant to your task.
- Run the retrieve against the org that has the latest version of those components.
- Run
git diffand read what changed. - Commit the manifest together with the source it produced, so the selection can be repeated.
<br>
Exercise
Your manifest lists ApexClass with * and CustomObject with Invoice__c. You run sf project retrieve start --manifest manifest/package.xml --target-org my-sandbox. A class OldHelper was deleted in the sandbox last week, but OldHelper.cls still exists in your project. Will this retrieve remove it, and where will a brand-new class from the org be written?
Show answer
The retrieve will not remove OldHelper.cls, because retrieve only writes and overwrites files and never deletes local ones. Use destructive changes or delete the file yourself. A brand-new class goes into the default package directory, the one marked "default": true in sfdx-project.json. Classes that already exist locally are overwritten where they are.