Sub topic 1
How deployments reach a target org
Prerequisites
This chapter assumes you already know the following:
- Basic Salesforce metadata concepts such as Apex classes, triggers, and custom objects.
- The difference between a sandbox and a production org.
- How to install the Salesforce CLI (
sf) and authorize an org. - Apex tests and code coverage are required for most production deployments. The details come in later chapters.
What a deployment is
A deployment copies metadata from a source, such as your local project, into a target org. Metadata is the configuration and code that define an org, for example Apex classes, custom objects, and page layouts. A deployment does not move records such as accounts or contacts.
Every deployment goes through the Metadata API. The Salesforce CLI, change sets, and CI tools are different front ends to it. When you start a deployment, Salesforce creates an asynchronous job. The job gets an ID that starts with 0Af. You can use that ID to check progress, and later chapters use it for quick deploy.
Choosing what to deploy
You tell the CLI which metadata to send in one of two ways:
- A manifest, which is a
package.xmlfile that lists metadata types and member names. - Source paths, which point at folders or files in your project.
This manifest lists one Apex class and its test class:
<?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>60.0</version>
</Package>The version value is the API version used to interpret the manifest. Keep it consistent with your project's sourceApiVersion.
Choosing the target org
The target org is the org that receives the changes. The CLI needs an authorized org, which you can refer to by username or alias. You pass it with --target-org. If you leave the flag out, the CLI uses the default target org from your configuration. Check that default before you deploy, because deploying to the wrong org is an easy mistake.
sf org list
sf config get target-orgThe first command lists the orgs you have authorized. The second shows the current default.
Running a deployment
This command deploys the manifest to a sandbox:
sf project deploy start \
--manifest manifest/package.xml \
--target-org my-sandboxThe CLI uploads the metadata, waits for the job, and prints the result. If your terminal disconnects or you set a short wait time, the job keeps running on the server. You can check it with sf project deploy report --job-id <id>, or in Setup under Deployment Status.
What happens on the server
Salesforce processes the deployment in order:
- It checks that the components are valid and that their dependencies exist in the target org or in the same deployment.
- It saves the components.
- If tests are required, it runs the Apex tests.
- It reports success or failure for the whole deployment.
A deployment is all or nothing. If any component fails, or a required test fails, Salesforce rolls back the entire deployment and the target org stays as it was. Partial results are not kept. This behavior is why a failed deploy to production does not leave the org half updated.
Why production is different
Sandboxes and production orgs differ in how they treat Apex tests:
| Target | Apex tests on deploy |
|---|---|
| Sandbox or scratch org | Not required by default |
| Production | Required when the deployment includes Apex classes or triggers |
In production, the default test level runs local tests when Apex is included, and the org must meet the code coverage requirement. These rules slow down a production release, because the tests can take many minutes. The rest of this tutorial shows how to run that slow part ahead of time.
Checkpoint
You should now be able to name the three inputs of any deployment: the metadata to send, the target org, and the test level. The next chapter covers the third input.
Exercise
You run sf project deploy start against production with a manifest containing one Apex class. A required test fails. What is the state of the production org, and what identifies the failed job?
Show answer
The production org is unchanged, because deployments are all or nothing and Salesforce rolls back everything when a required test fails. The job is identified by its deployment ID, which starts with 0Af. You can look it up with sf project deploy report --job-id or in Setup under Deployment Status.