Sub topic 13
Metadata API for deploying configuration
This chapter assumes you know Salesforce objects and fields, can read basic XML, and have used the REST or SOAP APIs from earlier chapters. The Metadata API works on the definition of an org, not on its records.
What metadata is
Metadata describes how an org is built: custom objects, fields, page layouts, Apex classes, flows, profiles, and permission sets. Data APIs such as REST and Bulk API 2.0 read and write records. The Metadata API reads and writes these definitions as XML files.
Its main use is moving configuration between orgs, for example from a sandbox to production. The Salesforce CLI, Ant migration tool, and change sets all use it underneath. Most developers call it through the CLI and not by writing raw SOAP requests.
How a deployment works
A deployment is asynchronous. You send a zip file, the API returns an ID, and you poll that ID until the deployment finishes.
The zip file has two parts:
- A
package.xmlmanifest that lists the components to deploy. - A folder per component type that holds the component files.
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>Account.Region__c</members>
<name>CustomField</name>
</types>
<types>
<members>AccountSummaryResource</members>
<name>ApexClass</name>
</types>
<version>60.0</version>
</Package>The manifest names each component type and its members. The version element sets the API version used to interpret the files.
Deploying with the Salesforce CLI
The CLI builds the request and polls for you. This command deploys the components named in the manifest and runs only the tests you list.
sf project deploy start \
--manifest manifest/package.xml \
--target-org my-sandbox \
--test-level RunSpecifiedTests \
--tests AccountSummaryResourceTestTo check a deployment without saving anything, add --dry-run. This validates the package against the org and runs the tests. Production deployments of Apex need tests, so a validation first lets you find failures before the release window.
Retrieve and deploy
The same API also works in the other direction. A retrieve call downloads components as files, which you can store in version control. This is the basis of source-driven development: the repository holds the configuration, and deployments push it to orgs.
sf project retrieve start \
--manifest manifest/package.xml \
--target-org my-sandboxCommon errors and limits
- Missing dependencies. A field that appears on a layout fails to deploy if the field is not in the package or the target org. Include dependencies or deploy them first.
- Test coverage. Deployments to production need at least 75% Apex code coverage, and every test that runs must pass.
- Partial deployments. By default a deployment is all-or-nothing. A single failed component rolls back the whole deployment.
- Size limits. A deployment package is limited to 39 MB and 10,000 files. Split larger packages into several deployments.
- Org differences. Settings, profiles, and permission sets may behave differently between orgs. Review those changes before you deploy them.
When to choose it
Use the Metadata API when the thing you are changing is configuration and it must move between orgs. Use the Tooling API (covered later) for finer-grained development tasks, such as querying Apex classes or running tests from an editor. Use the data APIs for records.
Exercise
A team wants to copy a new custom field and an Apex class from a sandbox to production, with Apex tests run on the way. Which API handles this, and what is the safest way to check the deployment before it changes production?
Show answer
The Metadata API, usually through the Salesforce CLI. Run the deployment as a validation (--dry-run) first. It checks the package and runs the tests without saving changes, so failures show up before the real deployment.