Sub topic 1
Metadata types and why a manifest is needed
This chapter explains what Salesforce metadata types are and why the Metadata API works from a manifest, a file that lists the components to act on. The later chapters on package.xml structure and destructive changes build on this model.
Prerequisites
This chapter assumes you have:
- Basic familiarity with Salesforce orgs and metadata types such as Apex classes and custom objects.
- The Salesforce CLI (
sf) installed and authorized to a sandbox or scratch org. - A Salesforce DX project with a
force-appsource directory. - Enough XML reading skill to follow an element with child elements and a namespace.
Run these commands against a sandbox or scratch org, not production. This chapter only reads from the org, but the habit matters for the chapters that delete things.
Data versus metadata
Salesforce separates two kinds of information.
- Data is the records users create, such as an Account named "Acme" or an Opportunity worth 50,000.
- Metadata is the configuration that defines how the org behaves and looks. It includes Apex classes, triggers, custom objects, custom fields, page layouts, flows, permission sets, and Lightning web components.
The Metadata API reads and writes metadata. It is a different API from the one that queries records. Tools that move configuration between orgs all use it. These tools include the Salesforce CLI, change sets (through a different interface), and the Ant Migration Tool.
Metadata is organized into metadata types. A type is a category of component with a fixed shape. ApexClass is a type, and AccountService is one component of that type. CustomObject is a type, and Invoice__c is a component of it.
Every component is identified by two values:
| Part | Meaning | Example |
|---|---|---|
| Type name | The API name of the metadata type | `ApexClass` |
| Full name | The unique name of the component within that type | `AccountService` |
The pair is how you refer to anything in the org. In the CLI you will see it written as ApexClass:AccountService. In XML it is split across a members element and a name element.
Discovering which types exist
Salesforce has well over 100 metadata types, and the list changes with each API version. Not every type is supported by every tool, and some types are available only if the org has a given feature enabled. Ask the org itself instead of guessing:
sf org list metadata-types --target-org my-sandboxThe output lists the type names the org supports at the CLI's API version. To see the components that exist for one type, use sf org list metadata:
sf org list metadata \
--metadata-type ApexClass \
--target-org my-sandboxEach result row includes the fullName of a component. That string is exactly what you put in a manifest, so copying it is safer than typing it from memory.
Salesforce also publishes a Metadata Coverage Report. It shows which types are supported by the Metadata API, by source tracking, and by packaging. Check it when a type behaves differently from what you expect.
Why a bare "deploy everything" is not the default
Suppose you want to move one Apex class from a sandbox to production. The Metadata API could in principle send the whole org's configuration, but that would be slow and risky for three reasons.
- Size. A mature org holds tens of thousands of components. A single retrieve or deploy is limited in file count and archive size, so a full copy often fails outright.
- Blast radius. Deploying components you did not intend to change overwrites production versions with whatever is in your source.
- Time. Deployments run Apex tests and compile code. More components means longer runs.
Instead, every retrieve and deploy is scoped. You tell the Metadata API exactly which components are involved. That description is the manifest.
What a manifest is
A manifest is an XML file, conventionally named package.xml, that lists metadata components by type and name. It does not contain the components themselves. It is a list of what to retrieve or what a deployment contains.
Here is a small one:
<?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>
<version>62.0</version>
</Package>Read it as: "In the type ApexClass, the components AccountService and AccountServiceTest, using API version 62.0." The next chapter takes each element apart. For now, notice the shape. Each types block holds one type name and any number of members of that type.
The manifest serves two roles.
- On retrieve, it is a request. The Metadata API reads it and returns files for each listed component.
- On deploy, it is the table of contents of the package you send. The API checks that the files in the package match what the manifest claims.
Without it, the API cannot tell which components you intend to act on.
The request flow
The diagram shows how a manifest sits between your project and the org.
In a DX project the CLI does some of this for you. If you deploy with --source-dir, the CLI works out the component list from the files and builds the manifest internally. The --manifest flag lets you supply the list explicitly. That is useful when the set of components does not map neatly to a folder, or when you want a reviewed, version-controlled record of what a release contains.
Source format versus Metadata API format
A common source of confusion in DX projects is that files on disk do not look like what the API sends.
- Source format is how your DX project stores metadata. A custom object is split into many small files: one per field, one per validation rule, and so on, under
objects/Account/fields/. - Metadata format is what the Metadata API expects. A custom object is one large
.objectfile that contains all its fields and rules inline.
The CLI converts between the two when it retrieves or deploys. The manifest always uses Metadata API names, regardless of how files are stored locally. So even though your project has a file Region__c.field-meta.xml, the manifest refers to it as the CustomField member Account.Region__c, not as a file path.
This is also why naming can go wrong. A path like force-app/main/default/classes/AccountService.cls is not a valid member name. The member is just AccountService.
Retrieving with a manifest
To see a manifest at work, retrieve the two classes listed above into your project:
sf project retrieve start \
--manifest manifest/package.xml \
--target-org my-sandboxThe CLI sends the manifest to the Metadata API, receives the component files, converts them to source format, and writes them under your package directory. If a listed member does not exist in the org, the CLI reports a warning for that member and continues with the rest. A typo in a member name therefore gives a partial retrieve rather than a total failure, so read the output.
You can also name components directly without a file:
sf project retrieve start \
--metadata ApexClass:AccountService \
--target-org my-sandboxBoth forms produce the same underlying request. The file form is better when the list is long or should be reviewed in a pull request.
Related types and partial results
Some types depend on other types to be useful. If you retrieve only Profile with no other types, the file you receive contains only the permissions for components that are also in the same request. A profile retrieved alone is nearly empty. To capture access to an Apex class, the manifest must include both the Profile and the ApexClass.
Permission sets and some settings types behave the same way. When a retrieved file looks incomplete, check whether the manifest also included the types it refers to.
Deploy is an upsert, not a sync
This is the main reason the rest of this tutorial exists. A normal deployment does two things:
- If a component in the package already exists in the target org, it is updated.
- If it does not exist, it is created.
Components in the org that are not in the package are left alone. Deleting a file from your project, committing, and deploying does nothing to the org, because the deployment never mentions the missing component. The org keeps it forever.
That gap is why the manifest idea extends to a second manifest-like file. The destructive changes file uses the same format as package.xml, but its members are "delete these" instead of "include these."
Common errors at this stage
| Symptom | Likely cause | Fix |
|---|---|---|
| Retrieve succeeds but a component is missing | The member name is misspelled or the component does not exist in that org | Check the name with `sf org list metadata` |
| Warning about an unknown type | The type name is misspelled, or the type is unavailable at the manifest's API version or in that org | Compare with `sf org list metadata-types` |
| Retrieved profile has almost no content | Related types were not in the manifest | Add the types the profile refers to |
| Deployed code is not in the org | Component is in a package directory not named in `sfdx-project.json`, or the manifest omitted it | Verify the path and the manifest |
| File path used as a member name | Source format confusion | Use the component's API name, not its path |
Summary
- Metadata is the configuration of an org. It is grouped into types, and each component is identified by its type and full name.
- The Metadata API acts on scoped lists of components, not on a whole org.
- A manifest, usually
package.xml, is that list. It names components and does not contain them. - Manifests use Metadata API names even though DX projects store files in source format.
- Deployments create and update but never delete, which is the reason destructive changes exist.
Exercise
You delete the file LegacyAccountService.cls from your DX project, commit the change, and run sf project deploy start --source-dir force-app. The deployment succeeds. Does the LegacyAccountService class still exist in the target org, and why?
Show answer
Yes, it still exists. A deployment creates or updates the components it contains and leaves everything else alone. Because the deleted file is no longer in the package, the deployment never mentions the class, so the org keeps it. Removing it requires a destructive changes file or a delete command.
Exercise
A teammate writes this member in a manifest: force-app/main/default/classes/AccountService.cls under the type ApexClass. What is wrong, and what should the member be? Which command can you use to list the valid member names in the org?
Show answer
A member must be the component's full name, not a file path. The member should be AccountService. You can list valid names with sf org list metadata --metadata-type ApexClass --target-org my-sandbox.