Sub topic 4
Generating a manifest from source or metadata names
This chapter assumes you know the structure of package.xml (types, members, name, version) 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. You will let the CLI write manifests for you, so you don't have to type every member name by hand.
Why generate a manifest
Typing member names is slow, and a typo gives you a wrong or empty manifest. A field must be written as Account.Region__c, for example, and the CLI can produce that format for you. The command sf project generate manifest has two input modes:
- From source: it reads the files in a directory of your project and lists the components it finds.
- From metadata names: you give it
Type:Namepairs and it writes them into the manifest.
Generating from a source directory
Use --source-dir to point at a folder in your project. The CLI scans it and writes one types block for each metadata type it finds.
sf project generate manifest \
--source-dir force-app \
--name manifest/package.xmlThe result is a normal manifest. It contains explicit member names, not wildcards, and the version element comes from sourceApiVersion in your sfdx-project.json. A small project might produce this:
<?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>You can point --source-dir at a single subfolder, such as force-app/main/default/classes. Then the manifest covers only that folder. You can also pass the flag more than once to combine folders.
Generating from metadata names
When you know what you want but have no local files for it, use --metadata. Each value has the form Type:Name, and you can repeat the flag.
sf project generate manifest \
--metadata ApexClass:AccountService \
--metadata CustomField:Account.Region__c \
--output-dir manifestThe names must match the Metadata API names exactly. The type is CustomField, not "field", and the field member includes its object. A misspelled name still produces a manifest. The error appears only when you retrieve or deploy with it.
Choose the input mode this way:
| Situation | Input to use |
|---|---|
| You want everything in a project folder | `--source-dir` |
| You want a handful of named components | `--metadata` |
| The components exist only in an org | `--metadata`, then retrieve |
Choosing the manifest type
By default the command writes a regular package.xml. The --type flag changes that. Besides package, it accepts pre, post, and destroy, which produce destructive changes files. Those files use the same XML format, so you can generate a deletion list from a source folder and edit it down to the components you really want to remove.
sf project generate manifest \
--source-dir force-app/main/default/classes/LegacyAccountService.cls \
--type post \
--output-dir manifestCheck the output name before you use it. Pre and post files get their own default names, and the CLI help lists them. Also remember that a destructive file lists exact names. The wildcard * is not supported there, so make sure the generated file contains no * entries.
Common errors
- The manifest is empty or missing types. The directory you passed has no recognized metadata, or the path is wrong. Run the command against
force-appfirst and compare. - A component is missing from the output. Only files inside the directory you named are scanned. Components in another package directory need their own
--source-dir. - The wrong API version. The generated
versionfollowssourceApiVersion. Edit it by hand, or updatesfdx-project.json, if you need a different one. - A stale manifest. The file is a snapshot. After you add or remove source files, regenerate it, or your deployment will use the old list.
Exercise
You run sf project generate manifest --source-dir force-app/main/default/classes and the manifest has no CustomField types, although your project has custom fields. Why, and how do you fix it?
Show answer
The command only scans the directory you pass, and custom fields are stored under the objects folder, not the classes folder. Pass --source-dir force-app to scan the whole package directory. You can also repeat --source-dir for the objects folder, or add the field with --metadata CustomField:Account.Region__c.