Sub topic 5
Running a Validation from the CLI
This chapter shows how to start a validation-only deployment with the Salesforce CLI, choose the right flags, and keep the output you need for a later quick deploy. It assumes you can already do the following:
- Use the
sfCLI and authorize orgs. Your production org has an alias,prodin these examples. - Read a
package.xmlmanifest and a source-format project (force-app/). - Explain what Apex test levels and the 75% coverage rule are. The earlier chapters cover them.
What the command does
sf project deploy validate sends your metadata to the target org and runs the same checks as a real deployment: component compilation, dependency checks, and Apex tests at the level you choose. At the end, the org rolls everything back, so nothing is saved. The org records the result as a deployment with a job ID that begins with 0Af. If the validation passes and the other conditions are met, you can promote it later with a quick deploy.
The command is a thin wrapper over the Metadata API's checkOnly deployment. It differs from sf project deploy start in two ways. It never saves changes, and it is built to feed the quick deploy step.
Since diagram fences must use the exact info string, here is the flow as a proper diagram.
Step 1: Define what to deploy
The CLI accepts three ways to select components. Use exactly one style per command.
| Flag | Selects | Typical use |
|---|---|---|
| `--manifest` (`-x`) | Components listed in a `package.xml` | Release packages that must match a reviewed list |
| `--source-dir` (`-d`) | Files in a folder or list of folders | Quick checks of one area of the project |
| `--metadata` (`-m`) | Named metadata such as `ApexClass:AccountService` | Small, targeted checks |
For releases, a manifest is the most repeatable choice because it can be reviewed in a pull request. This one 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>Save it as manifest/package.xml. The <version> element sets the API version used to interpret the package. Keep it consistent with sourceApiVersion in sfdx-project.json, or you may see unexpected differences between what you tested in a sandbox and what production receives.
Include the test classes in the package when the org does not already have them. In production, only tests that exist in the org (or arrive in this deployment) can run. A class that is not in the manifest and not already deployed cannot supply coverage.
Step 2: Run the validation
Run the validation against production with all local tests:
sf project deploy validate \
--manifest manifest/package.xml \
--test-level RunLocalTests \
--target-org prodThe parts of this command:
--manifestpoints at the package definition.--test-level RunLocalTestsruns every Apex test in the org except tests that come from installed managed packages. This is the usual level for production releases.--target-org prodis the alias or username of the org to validate against. It must be authorized withsf org login web(or another login method) first.
By default the CLI waits for the job to finish and prints progress. When it succeeds, you see a deploy ID (the 0Af job ID) and a line that the validation completed. Copy that ID immediately.
Choosing the test level
The --test-level flag takes these values on a validation:
| Value | Tests that run | Notes |
|---|---|---|
| `RunLocalTests` | All tests in the org except managed package tests | Common for production |
| `RunAllTestsInOrg` | Every test, including managed package tests | Slowest; seldom needed |
| `RunSpecifiedTests` | Only tests you name with `--tests` | Faster, but with stricter coverage rules |
Run sf project deploy validate --help to confirm the accepted values in your CLI version. NoTestRun makes no sense for a validation meant to feed quick deploy, because quick deploy only works when tests ran during validation.
With RunSpecifiedTests, name each test class with a repeated --tests flag:
sf project deploy validate \
--manifest manifest/package.xml \
--test-level RunSpecifiedTests \
--tests AccountServiceTest \
--target-org prodFor this level, the org checks that each class and trigger in the deployment has at least 75% coverage from the tests you named. That is stricter than the org-wide 75% average that applies to RunLocalTests. A package with a well-covered class and a poorly covered one fails here even if the overall average looks fine.
Step 3: Control waiting and output
Large orgs can take an hour or more to run all local tests. These flags control how the CLI behaves while it waits.
| Flag | Effect |
|---|---|
| `--wait <minutes>` | How long the CLI waits before returning. If the time expires, the job keeps running in the org |
| `--async` | Return right away with the job ID and do not wait |
| `--verbose` | Show per-component and per-test details |
| `--ignore-warnings` | Let warnings through instead of failing the validation |
| `--coverage-formatters` | Write coverage data in formats such as `html` or `json` |
| `--junit` | Write JUnit-style test results, useful for CI dashboards |
| `--results-dir` | Directory where the coverage and test files are written |
A --wait timeout is not a failure. The CLI stops watching, but the validation continues on the server. This is a common source of confusion: the terminal shows a timeout message, yet the job is still running and may pass.
To start a validation and check on it later, use --async:
sf project deploy validate \
--manifest manifest/package.xml \
--test-level RunLocalTests \
--target-org prod \
--asyncThe command prints the job ID right away. You can then poll it. The report command takes that ID:
sf project deploy report --job-id 0AfXXXXXXXXXXXXXXX --target-org prodsf project deploy report shows the current status (Pending, InProgress, Succeeded, Failed, Canceled), the number of components and tests processed, and any errors. If you ran a validation with --wait and your terminal closed, sf project deploy resume --job-id <id> reattaches to it, while report only prints a snapshot.
A fuller command for a CI job, writing results to files:
sf project deploy validate \
--manifest manifest/package.xml \
--test-level RunLocalTests \
--target-org prod \
--wait 120 \
--verbose \
--coverage-formatters json \
--junit \
--results-dir test-resultsStep 4: Record the job ID
The job ID is the handle for the whole later workflow. A quick deploy needs it, and so does any investigation of what happened. Do not rely on scrolling back through terminal output. Instead:
- Run with
--asyncor capture the output, and store the ID in your release ticket or CI artifact. - If you lose it, find it in Setup under Deploy > Deployment Status, where recent validations are listed.
- Note the time the validation finished. Quick deploy eligibility is time-limited (the next chapters cover this window).
The CLI also remembers the most recent deploy job in its local state. Commands such as report and quick accept --use-most-recent in place of --job-id. That is convenient on a laptop but risky in shared scripts, because a later deployment from the same machine replaces the cached job. In CI and release runbooks, pass the explicit ID.
Exit codes and scripting
The CLI exits with a non-zero code when a validation fails, so shell scripts and CI steps can stop automatically. A script can also read structured output with --json:
sf project deploy validate \
--manifest manifest/package.xml \
--test-level RunLocalTests \
--target-org prod \
--json > validation.jsonThe JSON includes the job ID, status, and counts of failed components and tests. Parse the ID from this file rather than from human-readable text, since the text format can change between CLI versions. Be aware that a JSON result from a failed validation still contains useful detail (for example, componentFailures and runTestResult), so keep the file as an artifact even when the step fails.
Common mistakes
- Validating against the wrong org. An alias such as
prodcan point to a different org than you expect. Runsf org listand check the username before a long validation. - Different metadata than the release. Validating from a local branch that is not the release branch produces a job ID that does not correspond to what you will promote. Validate from the exact commit you plan to ship.
- Treating a wait timeout as a failure. Check
sf project deploy reportbefore re-running. Starting a second validation while the first is still running can cause lock errors and wastes test time. - Using
RunSpecifiedTeststo save time, then failing coverage. The per-class 75% rule catches under-tested classes thatRunLocalTestswould hide in the average. - Missing test classes in the package. If your test classes live only in a sandbox, production cannot run them.
Putting it together
- Confirm the target:
sf org listand check thatprodis the org you intend. - Prepare
manifest/package.xmlfrom the release branch, including test classes. - Run
sf project deploy validatewith--test-level RunLocalTestsand--async(or a generous--wait). - Poll with
sf project deploy report --job-id <id> --target-org produntil the status isSucceededorFailed. - On success, store the job ID with the release record. The next chapters read the results in detail and explain when you can run the quick deploy command,
sf project deploy quick --job-id <id> --target-org prod.
Exercise
You run sf project deploy validate --manifest manifest/package.xml --test-level RunLocalTests --target-org prod --wait 10. After 10 minutes the terminal reports a timeout. Your teammate wants to re-run the command. What should you do first, and why?
Show answer
Run sf project deploy report --job-id <id> --target-org prod (or find the job under Setup > Deployment Status) to see the real status. The --wait timeout only stops the CLI from watching. The validation keeps running in the org and may still succeed. Starting a second validation could cause lock errors and repeat the test run. If it later shows Succeeded, record that job ID for the quick deploy.
Exercise
A package has two classes. One has 95% coverage and the other has 60%, so the combined average is above 75%. Does a validation with RunLocalTests pass the coverage check, and does one with RunSpecifiedTests pass? Explain the difference.
Show answer
With RunLocalTests, the 75% rule applies to the org-wide average, so the combined figure can satisfy it. With RunSpecifiedTests, each class and trigger in the deployment must reach 75% from the specified tests, so the 60% class makes the validation fail.