Sub topic 5
Running a Validation from the CLI
This chapter assumes you can already authorize an org with the Salesforce CLI (sf) and that you know what a manifest (package.xml) is. You should also know why production deployments run Apex tests. Here you run a validation-only deployment and see what the command does.
What the command does
A validation-only deployment sends your metadata to the target org and runs the same checks as a real deployment, including the Apex tests. At the end, Salesforce rolls everything back. Nothing is saved. The result is a job record with an ID that you can use later for a quick deploy.
The command is sf project deploy validate. It is the CLI equivalent of the older --dry-run flag on sf project deploy start. Prefer validate, because it is the command that produces a deployment that can be quick deployed.
Step 1: Write a manifest
The manifest lists the components to validate. This one contains 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 in your project. The version element is the API version used to interpret the manifest.
Step 2: Run the validation
sf project deploy validate \
--manifest manifest/package.xml \
--test-level RunLocalTests \
--target-org prodEach flag matters:
--manifestpoints to the components to check. You can use--source-diror--metadatainstead.--test-levelchooses which Apex tests run.RunLocalTestsruns every test in your org except tests from managed packages. This is the usual choice for production.--target-orgis the alias or username of the org you authorized. Here it is production.
The CLI waits for the job to finish by default and prints progress. If you want your terminal back sooner, add --wait with a number of minutes. If the wait expires, the job keeps running on the server.
Step 3: Follow the job
The sequence below shows what happens between your terminal and the org.
The job ID starts with 0Af. Copy it as soon as it appears. If your terminal closes or the wait times out, you can still check the job with the report command:
sf project deploy report --job-id 0AfXXXXXXXXXXXXXXX --target-org prodReplace the placeholder with your real ID. The report shows the status, the number of components deployed, and test results.
Choosing flags that affect later steps
Your choices in this command decide whether quick deploy is possible later.
| Choice | Effect |
|---|---|
| `--test-level RunLocalTests` | Runs all local tests. Meets the production requirement and allows quick deploy. |
| `--test-level RunSpecifiedTests` with `--tests` | Runs only the named tests. In production, each class or trigger in the deployment still needs enough coverage, so a failed coverage check is common. |
| `--test-level NoTestRun` | Skips tests. Not valid for production deployments that include Apex. |
| `--test-level RunAllTestsInOrg` | Runs everything, including managed package tests. Slower, and rarely needed. |
Later chapters cover the exact quick deploy rules. For now, remember that a validation which did not run the required tests cannot be promoted.
Common errors at this step
- Authorization errors. If the CLI says it cannot find the org, run
sf org listand check that the alias matches. Re-authorize withsf org login webif the token has expired. - Missing component. A name in the manifest that does not exist in your local project gives a "no source-backed components" or "not found" error. Check spelling and that the file is in a package directory listed in
sfdx-project.json. - Test failures. The job ends as
Failedand the output lists each failing test with its message and stack trace. Fix the cause locally and validate again. A failed validation never gives you a job ID to promote. - Timeout confusion. A wait timeout in the CLI does not cancel the job. Use the report command before you start a new validation.
Exercises
Exercise
You run sf project deploy validate against production and your terminal loses its connection before the command finishes. What should you do before starting another validation, and why?
Show answer
Run sf project deploy report --job-id <id> --target-org prod with the job ID printed earlier. The job continues on the server after the CLI stops waiting, so it may already have finished or still be running. Starting a second validation could duplicate the work or conflict with the first.
Exercise
A teammate validates with --test-level NoTestRun against production for a manifest containing an Apex class. The command is rejected. Which command line would you use instead for a standard validation?
Show answer
sf project deploy validate --manifest manifest/package.xml --test-level RunLocalTests --target-org prod. Production deployments that include Apex require tests, and RunLocalTests is the usual level.