Sub topic 1
Metadata types and why a manifest is needed
Prerequisites
This chapter assumes you have the following:
- 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. - Basic XML reading skills.
- A Salesforce DX project with a
force-appsource directory.
You do not need to have written a manifest before. This chapter explains why one exists. Later chapters show how to write one.
What the Metadata API manages
An org has two kinds of content. Data is the records users create, such as accounts and contacts. Metadata is the configuration that defines how the org behaves, such as Apex classes, custom fields, page layouts, and flows.
The Metadata API is the interface that the Salesforce CLI uses to read and write that configuration. It has two core operations:
- Retrieve copies metadata from an org to your local machine.
- Deploy sends metadata from your machine to an org.
Every component the API handles belongs to a metadata type. The type tells the API what kind of thing the component is. Some common types are shown below.
| Metadata type | Example component |
|---|---|
| `ApexClass` | `AccountService` |
| `ApexTrigger` | `AccountTrigger` |
| `CustomObject` | `Invoice__c` |
| `CustomField` | `Account.Region__c` |
| `Layout` | `Account-Account Layout` |
| `Flow` | `Update_Region` |
Each type has its own naming rules. Chapter 3 covers them in detail. For now, remember that a component is identified by its type and its name together. AccountService could be a class or a flow, and the API needs to know which one you mean.
Why the API needs a manifest
An org can hold thousands of components. When you ask the API to retrieve or deploy, it cannot guess which ones you want. It has to receive an explicit list.
That list is the manifest, and by convention the file is named package.xml. It names each metadata type and the components of that type you want to include. Without it, the API has no scope for the request.
The manifest solves three practical problems:
- Scope. You can move a few components instead of the whole org.
- Repeatability. The same file produces the same request each time. You can store it in version control and review it like code.
- Version control of the API. The manifest states which API version to use, so a request behaves the same way on different days.
The manifest also matters for deletion. The Metadata API has no "delete" command that stands alone. Components are removed during a deployment, by listing them in a second manifest-style file. That file is called a destructive changes file, and it has the same format as package.xml. If you understand the manifest, you already understand most of the destructive file.
How the pieces fit together
The CLI reads the manifest, builds a request, and sends it to the Metadata API. The API then works on the org.
A first look at a manifest
Here is a small manifest. It selects two Apex classes and one custom field.
<?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>Read it from the inside out. Each <members> element names one component. The <name> element that follows names the metadata type those members belong to. The <version> element sets the API version. The next chapter walks through each element.
Common mistakes at this stage
- Treating the manifest as a list of names only. A name without its type is ambiguous. Always group members under a type.
- Expecting the manifest to change the org by itself. The file only describes a selection. Nothing happens until you pass it to a retrieve or deploy command.
- Assuming a deployment removes components that are missing from the manifest. A normal deployment adds and updates. Leaving a component out of
package.xmldoes not delete it. Deletion needs a destructive changes file.
Exercise
You deploy a manifest that lists AccountService under ApexClass. The org also contains an Apex class named LegacyAccountService, which the manifest does not mention. What happens to LegacyAccountService, and what would you need to change that?
Show answer
Nothing happens to it. A normal deployment only adds or updates the components it lists, so omitting a component does not delete it. To remove LegacyAccountService, you must list it in a destructive changes file and include that file in the deployment.
Exercise
Why is a component name such as AccountService not enough to identify a component in a manifest?
Show answer
Different metadata types can use the same name, such as an Apex class and a flow. The API identifies a component by its type and name together, so each member must sit under the correct <name> type element.