Sub topic 1
How Deployments Reach a Target Org
Prerequisites
This chapter assumes you already know the following:
- Basic Salesforce metadata concepts: 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 with it. - Apex tests and code coverage are required for most production deployments. You don't need the details yet. Later chapters cover them.
This chapter sets up the vocabulary for the rest of the tutorial. It covers what a deployment is, what happens between your command and the target org, and why the target org changes how the deployment behaves. Later chapters build on it: test levels, validation-only deployments, and quick deploy.
What a deployment is
A deployment moves metadata from one place to a target org. The source can be a local project, a version control repository, or another org. The target org is the org that receives and saves the changes.
Metadata is the configuration and code that define an org. Examples are Apex classes, triggers, custom objects, flows, and permission sets. Data, such as account records, is not metadata and is not moved by these tools.
A deployment is not a copy that always succeeds. The target org checks every component, and it may run Apex tests. If any component fails, the whole deployment fails (more on this below).
The path from your command to the org
When you run a deploy command, the CLI does not push files one at a time. It follows these steps:
- The CLI reads what you asked to deploy. This can be a manifest file, a list of directories, or a list of named metadata components.
- It converts the source-format files in your project into metadata format. This is the layout the Metadata API expects.
- It zips the converted files and sends them to the target org's Metadata API as a deploy request.
- The org queues the request and returns a job ID. A deploy job ID starts with
0Af. - The org processes the job. It saves components, runs tests if required, and records the result.
- The CLI polls the org for status until the job finishes or its wait time runs out.
The job ID matters more than it first appears. The job runs on the server, so it keeps going even if your terminal closes. Later you will use that same ID to check status and, after a successful validation, to run a quick deploy.
Source format and metadata format
Projects created for Salesforce DX use source format. A custom object is split into small files, for example one file per field. The Metadata API uses metadata format, where an object is usually one large XML file.
You do not convert files by hand. The CLI does it as part of each deploy. This is why a manifest (package.xml) works in both formats. It lists component types and names, and the CLI finds the matching files in your project.
Choosing what to deploy
The CLI offers three ways to select components. They can be combined.
| Option | Example | Best for |
|---|---|---|
| Manifest | `--manifest manifest/package.xml` | Repeatable releases with a reviewed list of components |
| Source directory | `--source-dir force-app/main/default/classes` | Deploying everything in a folder |
| Metadata name | `--metadata ApexClass:AccountService` | Small, one-off deployments |
For release work, a manifest is usually the best choice. It is a file you can review, commit, and reuse. The same manifest then drives both the validation and the final deployment, which keeps them identical.
Here is a minimal manifest that lists an 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 element is the API version used to interpret the components. Keep it consistent with the version in your project's sfdx-project.json so you do not get surprises from version differences.
The target org
The target org is the org you name with --target-org. You can use a username or an alias. The CLI must already be authorized to it.
Authorize two orgs and give each an alias:
sf org login web --alias dev-sandbox --instance-url https://test.salesforce.com
sf org login web --alias prod --instance-url https://login.salesforce.com
sf org listSandboxes log in through test.salesforce.com. Production and Developer Edition orgs use login.salesforce.com. If you use a custom My Domain, pass your My Domain URL instead.
Always confirm the alias before you deploy. A deploy command sent to the wrong alias is a common and costly mistake. If you set a default target org for a project, the CLI uses it whenever you leave out --target-org. That is convenient in a sandbox and risky if the default is production. For release work, name the org explicitly in every command.
Why the org type changes the rules
Salesforce applies stricter checks to production than to sandboxes. The deploy command is the same, but the defaults differ.
| Behavior | Sandbox or scratch org | Production org |
|---|---|---|
| Default test level when the deployment includes Apex | No tests run | Local tests run |
| Default test level with no Apex in the deployment | No tests run | No tests run |
| Minimum code coverage to deploy Apex | Not enforced | 75% across the org's Apex |
| Triggers | No coverage required | Each trigger needs some coverage |
The practical effect is that a deployment that works in a sandbox can fail in production because tests or coverage are checked there. The rest of this tutorial exists to help you find that failure before your release window. The next chapters explain test levels and coverage in detail.
Deployments are all-or-nothing
By default a deployment is atomic. If any component fails to deploy, or any required test fails, the org rolls back the entire deployment. None of the components are saved, including the ones that passed. Production always works this way.
This has two consequences for planning:
- A single typo in one component can block an otherwise good release.
- A failed deployment leaves the org in the same state as before. You can fix the problem and run the deployment again.
Sandboxes can be set to allow partial saves with the Metadata API option rollbackOnError. Treat that as a special case. The model to learn for production is all-or-nothing.
Running a deployment
This command deploys the manifest to a sandbox and waits up to 30 minutes for it to finish:
sf project deploy start \
--manifest manifest/package.xml \
--target-org dev-sandbox \
--wait 30The --wait value is in minutes. If the job takes longer, the CLI stops waiting and returns control to you, but the job keeps running in the org. This is a frequent source of confusion. A timeout in your terminal does not mean the deployment failed.
To deploy to production with the tests that production usually requires, name the test level explicitly:
sf project deploy start \
--manifest manifest/package.xml \
--target-org prod \
--test-level RunLocalTests \
--wait 60This command saves changes in production if it succeeds. In this tutorial, you will use it rarely. You will usually run a validation first, and then promote it with quick deploy.
Checking on a job
You can check the status of any job with its ID:
sf project deploy report --job-id 0AfXXXXXXXXXXXXXXX --target-org prodThe report shows the status (for example InProgress, Succeeded, or Failed), the number of components deployed, and the number of tests run. Replace the placeholder with a real ID from your own deployment.
If you started a deployment and lost the terminal, you do not have to start over. Use the report command with the job ID, or open Deployment Status in Setup, which lists recent deployments with the same IDs.
Deployment locks and queueing
An org processes deployments one at a time. If a deployment is already running, a new request waits in the queue as Pending. A long test run, for example in a large production org, can delay everything behind it.
Two things follow from this:
- Do not start a second deployment to "speed things up." It joins the queue and can interfere with your own release.
- Time spent running tests directly extends the time your release holds up the queue. Reducing that time is the main reason quick deploy exists.
You can also cancel a running deployment if you started it by mistake. Cancelling is best done early, because a deployment that has already started saving components may be past the point where it can be stopped cleanly.
Common mistakes at this stage
- Relying on a default target org. The command succeeds, but against the wrong org.
- Treating a CLI timeout as a failure. The job may still be running. Check it with
sf project deploy report. - Testing only in a sandbox with no tests. A sandbox deploy with the default no-test level proves little about production, where tests run.
- Leaving a component out of the manifest. A class that depends on a field you did not include will fail. Include dependencies, or make sure they already exist in the target org.
- Mismatched API versions. A manifest version that differs from the project version can change how some components are interpreted.
What comes next
You now have the model: a deploy request is converted, sent to the target org's Metadata API, queued, processed as a single all-or-nothing job, and tracked by a job ID. Production adds test and coverage checks to that same process. The next chapter looks at the test levels you can choose, because the test level is the main setting that controls how long a production deployment takes and whether it can pass.
Exercise
You run sf project deploy start --manifest manifest/package.xml --wait 5 against production. The CLI stops after five minutes and prints that the wait time was exceeded. A teammate says the deployment failed, so you should start it again. What should you do instead, and why?
Show answer
Do not start it again. The wait time only limits how long the CLI waits. The job keeps running in the org, and a second request would queue behind it. Use the job ID printed by the CLI with sf project deploy report --job-id <ID> --target-org prod, or open Deployment Status in Setup, to see whether the job is in progress, succeeded, or failed. Start a new deployment only if the report shows it failed or you cancelled it.
Exercise
A deployment containing one Apex class and its test passes in your sandbox but fails in production because of a missing test or coverage problem. Give two reasons the sandbox did not catch it.
Show answer
First, the default test level in a sandbox is no tests, so no tests ran and coverage was never checked. Second, the 75% org-wide coverage rule and the requirement that each trigger have some coverage apply to production, not to sandboxes. Running the deployment with an explicit test level, or validating it against production, would have shown the problem earlier.