Sub topic 6
Reading results and keeping the job ID
This chapter assumes you can run sf project deploy validate against an authorized org, as covered in the previous chapter. You should know what a test level is and why production deployments need code coverage. Here you learn to read what a validation reports, and how to record the one value you need afterward: the job ID.
Why the result matters
A validation runs the full deployment process against the target org, then rolls everything back. The output is the only thing you keep. It tells you three things:
- Whether the metadata can deploy.
- Whether the Apex tests passed and coverage is sufficient.
- Which ID identifies this run, so you can quick deploy it later.
If you misread the result, you may plan a release around a validation that cannot be promoted. If you lose the job ID, you can still find it, but it costs time that you may not have inside a release window.
Anatomy of a validation run
When you start a validation, the org creates a deployment record. The CLI prints its ID right away. The ID is a Salesforce record ID that starts with 0Af, the key prefix for the DeployRequest object. It is the handle for everything that follows: reporting, canceling, resuming, and quick deploying.
sf project deploy validate \
--manifest manifest/package.xml \
--test-level RunLocalTests \
--target-org prod \
--wait 60With --wait 60, the CLI waits up to 60 minutes and shows progress for components and then tests. The run moves through three phases:
- Component deployment: every component in the manifest is checked against the org.
- Apex tests: the tests selected by the test level run.
- Coverage and final status: the org evaluates coverage and sets the final status.
A component error in phase one can end the run before any tests start. A run that fails during tests has already proven the components deploy, which narrows your debugging.
Reading the status
The deployment status is the first thing to check. Do not rely on the CLI printing text that looks green.
| Status | Meaning | What to do |
|---|---|---|
| Pending | The request is queued behind other deployments. | Wait. |
| InProgress | Components or tests are running. | Wait, or check with `report`. |
| Succeeded | Everything passed, including tests and coverage. | Record the job ID. Check quick deploy eligibility. |
| SucceededPartial | Only seen in some deployment modes, not for a normal validation. | Treat it as not ready until you understand it. |
| Failed | A component, a test, or coverage failed. | Read the failure sections, fix, and run a new validation. |
| Canceling or Canceled | Someone stopped the run. | Start a new validation if you still need one. |
Only a Succeeded validation can be a candidate for quick deploy. A Failed validation never can, so there is no reason to keep its ID except for investigating the failure.
The sections of the human-readable output
The default CLI output for a finished validation has a few labeled tables. Which ones appear depends on what happened.
Deployment summary
This shows the deploy ID, the target org, the status, and the number of components processed. Check that the target org is the one you intended. A validation against a sandbox does not qualify you to quick deploy to production, because quick deploy only works in the org where the validation ran.
Component failures
If any component failed, you get a table with the component type, name, file or problem, and the error message. The messages are the same ones you would get from a real deployment. Typical examples are a missing dependency, a field that does not exist in the target org, or a compile error in Apex.
Fix the first error before you look at the later ones. A missing custom field often causes several other errors in classes and layouts that reference it.
Test failures
Each failed test method is listed with its class, method name, the error message, and a stack trace. Read the stack trace from the top. The first line that names one of your classes is usually the useful one.
A test that passes in a sandbox and fails in production often depends on data or configuration that differs between the two orgs. Tests that rely on existing org data, rather than creating their own, are the usual cause.
Code coverage warnings
These appear when a class or trigger falls below the required coverage, or when the org-wide figure is too low. The rules themselves were covered earlier. In the results, read the warning as a list of specific components and their coverage percentages. The fix is almost always more or better tests for those named components, not a change to the test level.
Getting structured output
For anything beyond a quick look, use --json. The JSON is stable enough to script against, and it contains the same information as the tables.
sf project deploy validate \
--manifest manifest/package.xml \
--test-level RunLocalTests \
--target-org prod \
--wait 60 \
--json > validation.jsonThe fields you will use most often are under result:
| Field | What it tells you |
|---|---|
| `id` | The job ID, starting with `0Af`. |
| `status` | The status from the table above. |
| `success` | `true` only if the run fully passed. |
| `checkOnly` | `true` for a validation. If this is `false`, you ran a real deployment. |
| `numberComponentErrors` | Count of failed components. |
| `numberTestsCompleted` and `numberTestErrors` | How many tests ran and how many failed. |
| `details.runTestResult.codeCoverageWarnings` | Coverage problems, with component names. |
The checkOnly field is worth checking. It is the quickest way to confirm that a saved result came from a validation and not from a deployment that changed the org.
Keeping the job ID
The job ID is the only thing that links a successful validation to a later quick deploy. Treat it like a release artifact, and record it where the person doing the release will look.
Capture it in a script
This script runs a validation, saves the full JSON, and writes the ID to a file only when the validation succeeded. A CLI command that fails returns a nonzero exit code, so the script records that code and does not stop on it.
#!/usr/bin/env bash
set -uo pipefail
ORG="prod"
sf project deploy validate \
--manifest manifest/package.xml \
--test-level RunLocalTests \
--target-org "$ORG" \
--wait 60 \
--json > validation.json
exit_code=$?
JOB_ID=$(jq -r '.result.id // empty' validation.json)
STATUS=$(jq -r '.result.status // "unknown"' validation.json)
echo "Job ID: ${JOB_ID:-none}"
echo "Status: ${STATUS}"
if [ "$exit_code" -eq 0 ] && [ "$STATUS" = "Succeeded" ]; then
echo "$JOB_ID" > validated-job-id.txt
echo "Saved job ID to validated-job-id.txt"
else
echo "Validation did not succeed. Not saving a job ID." >&2
exit 1
fiThe script needs jq installed. Only a succeeded validation writes validated-job-id.txt, so a stale or failed ID can never be mistaken for a good one.
Record context with the ID
An ID alone is not enough for someone else to act on it. Write down at least these items next to it:
- The target org alias or username, since quick deploy must run in the same org.
- The date and time the validation finished. Quick deploy eligibility expires, and the next chapter explains the window.
- The test level used.
- The commit or manifest version that was validated.
A ticket comment or a release checklist entry works well for this. Commit hashes matter because the validation covers the exact contents deployed at that moment. If the source changes afterward, the validated ID no longer describes what you are about to release.
Recover a lost ID
If you did not save the ID, you have two ways to get it back.
The CLI remembers recent deployments on your machine. You can report on the most recent one:
sf project deploy report --use-most-recent --target-org prodThe cache only covers deployments started from that machine and project. It will not help if a colleague or a CI runner started the validation.
In that case, open Setup, search for Deployment Status, and look at the Succeeded list. Each row shows the deployment and, in the details, its ID. The next chapters cover the UI in more detail.
Checking a run that is still going
If the --wait time runs out, the CLI exits, but the validation keeps running in the org. The job is not lost. Check on it with the job ID:
sf project deploy report --job-id 0AfXXXXXXXXXXXXXXX --target-org prodTo rejoin and wait for it to finish, use sf project deploy resume --job-id 0AfXXXXXXXXXXXXXXX --wait 60. If you want to stop a run that is taking too long or was started in error, sf project deploy cancel --job-id 0AfXXXXXXXXXXXXXXX requests cancellation. The status passes through Canceling before it becomes Canceled.
Common mistakes when reading results
- Reading only the exit code. A zero exit code is a good sign, but also confirm that the status is
SucceededandcheckOnlyistrue. - Saving the ID from a failed run. It looks the same as a good one, but it can never be quick deployed.
- Validating against one org and planning to quick deploy to another. The ID is valid only in the org that created it.
- Reading the coverage percentage as one number. The gating rules apply per component and org-wide, so read the named warnings.
- Letting source change after validation. A new commit means the validated ID no longer matches what you intend to release.
Putting it together
A repeatable routine looks like this:
- Run the validation with
--jsonand a generous--wait. - Check
status,success, andcheckOnly. - If it failed, read component errors first, then test failures, then coverage warnings.
- If it succeeded, save the job ID with the org, test level, finish time, and commit.
- Hand the ID to whoever will run the quick deploy.
Exercise
A teammate says, "My validation printed no errors and the command returned, so we are ready to quick deploy." The CLI had a 10 minute wait and the validation ran Apex tests. Name two things you would check before agreeing, and one way to find the job ID if nobody saved it.
Show answer
First, check that the status is Succeeded and not still InProgress or Pending. A short --wait can expire while the validation keeps running on the server, so the command can return without a final result. Second, check that checkOnly is true and the target org is the production org you plan to deploy to, since a job ID only works in the org where it ran. If nobody saved the ID, run sf project deploy report --use-most-recent --target-org prod if the validation was started from that machine. Otherwise open Setup, go to Deployment Status, and find the validation in the Succeeded list.
Exercise
Why does the script in this chapter write the job ID to a file only when the status is Succeeded and the exit code is zero?
Show answer
A failed validation also has a job ID, and it looks identical to a good one. If the script saved every ID, someone could later pick up a failed or canceled ID and try to quick deploy it, which is never possible. Writing the file only on success means the file's existence is itself a signal that a promotable validation exists.