Sub topic 5
Validating first and using quick deploy
This chapter assumes you can read a package.xml, run sf project deploy start against an org, and understand test levels such as RunLocalTests. Earlier chapters covered what Salesforce does when a deployment fails and how to capture a baseline. Here you will use a validation to catch problems before they reach production, then promote it with quick deploy.
Why validation belongs in a rollback plan
The cheapest rollback is the one you never run. A validation is a deployment that executes the full compile, dependency check, and test run against the target org and then discards the result. Nothing is saved. Production metadata stays exactly as it was.
Validation prevents one specific class of problem: a deployment that fails partway through the release window. As an earlier chapter explained, a failed deployment is all-or-nothing, so the org is not damaged. The cost is time. If a deployment fails 40 minutes into a test run, you have lost the release window and may have to ask for another one.
Validation does not prevent the other class of problem, a deployment that succeeds and then behaves badly. A validation proves that the metadata compiles and that your tests pass. It cannot show that a new validation rule blocks a business process your tests never touch. You still need the baseline and the redeploy path from the other chapters.
What a validation does
The CLI runs sf project deploy validate against the target org and sets the Metadata API checkOnly option. Salesforce then does the following:
- Checks the package contents against the org, including dependencies and field-level conflicts.
- Compiles Apex.
- Runs the tests required by the test level you chose.
- Calculates code coverage for the deployed Apex.
- Reports success or failure, then discards the changes.
If the validation succeeds, Salesforce keeps a record of it under its job ID. That ID is what you later pass to quick deploy.
Run it against the real target
Validate against the org you will deploy to, not a sandbox you expect to behave the same. Production has its own data, installed packages, test classes that may be sensitive to data, and org settings. A change that passes in a full sandbox can still fail in production. Validating against production removes that uncertainty.
Choose a test level that qualifies
Quick deploy only accepts a validation that ran tests. In practice this means the validation must have used a test level that runs tests in the org, and those tests must have passed and met the coverage requirements. NoTestRun does not qualify. The earlier chapter's examples use RunLocalTests, which is the usual choice and the one used here.
| Test level | Runs | Usable for quick deploy |
|---|---|---|
| `NoTestRun` | No tests | No |
| `RunSpecifiedTests` | Only the tests you name | Yes, if the tests pass and the coverage requirements for the deployed Apex are met |
| `RunLocalTests` | All tests in the org except those from installed managed packages | Yes |
| `RunAllTestsInOrg` | All tests, including managed packages | Yes |
Check the current Salesforce documentation for the rules that apply to your API version, because the requirements for specified tests have changed over time.
Quick deploy
Quick deploy takes a recently successful validation and deploys the same package without running the tests again. The test run is usually the longest part of a production deployment. Skipping it can shrink a window of an hour or more to a few minutes.
Several conditions apply:
- The validation must have been run against the same org you are deploying to.
- The validation must have succeeded with tests, as described above.
- The validation must be recent. Salesforce honors a validation for 10 days.
- You deploy what was validated. Quick deploy uses the package that was uploaded at validation time. It does not read your working directory again.
The last point matters for rollback. Quick deploy is tied to a specific package, so the commit you validated is the commit you ship. If a teammate merges another change after the validation, that change is not part of the quick deploy. It would need its own validation.
Quick deploy and org drift
Between validation and quick deploy, other people may change production. An admin might edit a field, or another team might deploy. The validation result does not follow those changes. Treat the 10-day limit as a ceiling, not a target. Many teams validate the day before or the morning of the release, so the validated state and the production state are close.
If another deployment lands in the gap, the safe response is to validate again. A failed quick deploy is a failed deployment, so the org is unchanged. It still wastes your window.
The release sequence
The baseline comes first so that it reflects production before you validate. If you retrieve it after other changes land, it no longer matches what you will overwrite.
Doing it with the CLI
The first script captures the baseline, validates, and saves the job ID. Run it before the release window.
#!/usr/bin/env bash
set -euo pipefail
ORG=prod
MANIFEST=manifest/package.xml
sf project retrieve start \
--manifest "$MANIFEST" \
--target-org "$ORG" \
--output-dir backup/pre-release
sf project deploy validate \
--manifest "$MANIFEST" \
--target-org "$ORG" \
--test-level RunLocalTests \
--wait 60 \
--json > validate-result.json
jq -r '.result.id' validate-result.json > validated-job-id.txt
echo "Validated job ID: $(cat validated-job-id.txt)"Because of set -e, the script stops if the validation fails. The error details are then on screen, not in your job ID file. Inspect a failed validation with sf project deploy report and the job ID, as covered in the failure report chapter.
During the release window, a second script runs the quick deploy:
#!/usr/bin/env bash
set -euo pipefail
ORG=prod
JOB_ID=$(cat validated-job-id.txt)
sf project deploy quick \
--job-id "$JOB_ID" \
--target-org "$ORG" \
--wait 30sf project deploy quick creates a new deployment with its own job ID. Record that new ID too, because it is the one you would pass to sf project deploy cancel or sf project deploy report if something goes wrong during the deployment. The validation's ID is only the input.
Validation does not make the release safe
A validation covers compilation, dependency resolution, and your tests. Several kinds of harm get through all three.
- Untested behavior. A new flow, validation rule, or page layout change may have no test coverage. It deploys cleanly and then changes how users work.
- Data-dependent behavior. Tests create their own data in most orgs. Production data can trigger a code path that your tests never reached.
- Permissions and profiles. A profile deployment can validate cleanly and still remove access from a group of users.
- Irreversible changes. Data modifications made by Apex, or metadata changes that cannot be redeployed back, are not rolled back by redeploying the previous version.
The next chapters handle recovery for these cases. The point here is that a green validation means "this package will deploy", not "this release is correct".
Common errors
| Symptom | Likely cause | What to do |
|---|---|---|
| Quick deploy reports that the validation cannot be used | The validation ran with `NoTestRun`, failed, or is older than 10 days | Run a new validation with a qualifying test level |
| Quick deploy says the job ID is not found | The ID belongs to a different org, or you passed the ID of a regular deployment | Check the target org and use the validation's job ID |
| Validation passes but quick deploy fails | Production changed after validation, so a dependency or component state no longer matches | Read the failure report, then validate again |
| Validation times out | Test run exceeds `--wait` | Use `--async`, then check progress with `sf project deploy report` |
| Coverage failure at validation | Deployed Apex lacks sufficient test coverage | Fix the tests and revalidate. Do not move to quick deploy |
Waiting versus polling
For long validations, --wait 60 keeps your terminal blocked for up to 60 minutes. A different option is --async, which returns the job ID right away:
sf project deploy validate \
--manifest manifest/package.xml \
--target-org prod \
--test-level RunLocalTests \
--async
sf project deploy report --use-most-recent --target-org prodThe --use-most-recent flag refers to the last deployment job the CLI started in this project. It is convenient but easy to misuse if you ran another deployment in between. For scripts, pass --job-id explicitly.
Relationship to the rest of the rollback plan
Validation and quick deploy fit into the recovery paths from this tutorial as follows:
- Before the deployment: validation reduces the chance of a failed deployment and the need to cancel one.
- During the deployment: quick deploy shortens the time the deployment runs, so there is less time in which to notice a problem and cancel.
- After the deployment: if the release is successful but harmful, the quick deploy does not help. You return to the baseline you retrieved and redeploy it, using destructive changes for added components.
A useful rule is that the same package you validated and quick-deployed should also have a prepared reverse package. Build the baseline and the destructive changes manifest during the validation stage, not at 2 a.m. after a failure.
Exercise
Your team validated a release on Monday with --test-level NoTestRun because the test suite was slow. On Thursday, during the release window, you run sf project deploy quick with the Monday job ID. What happens, and what should the team do differently?
Show answer
The quick deploy is rejected because the validation did not run tests, so it does not qualify. The org is unchanged, but the release window is spent. The team should validate with a qualifying test level such as RunLocalTests before the window, and pass that validation's job ID to quick deploy. They should also retrieve a baseline before validating, so a rollback source exists if the release succeeds but causes problems.
Exercise
A quick deploy succeeds, and an hour later users report that a new validation rule blocks order entry. Did validation or quick deploy give you a way to undo this? Which recovery steps apply?
Show answer
No. Validation and quick deploy only affect whether and how fast the package deploys. To recover, redeploy the baseline you retrieved before the release (or revert the release merge commit in Git and deploy the result). Delete any components the release added by listing them in a destructive changes manifest. Check for data changes that a metadata redeploy cannot reverse.