Sub topic 4
What a validation does and does not do
This chapter assumes you know basic Salesforce metadata (Apex classes, triggers, custom objects) and the difference between a sandbox and a production org. It also assumes you can authorize an org with the sf CLI. Test levels and code coverage were covered in the previous two chapters. This chapter uses them without re-explaining them.
The idea in one paragraph
A validation-only deployment is a real deployment that is rolled back at the end. Salesforce runs the same checks and the same Apex tests it would run for a real deployment, then discards the result instead of committing it. You learn whether the deployment would succeed, and the target org's metadata stays as it was.
What happens during a validation
A validation goes through the same stages as a real deployment, in the same order. Only the last stage differs.
- The deployment request is queued on the target org.
- Salesforce reads the package and checks that the components are well formed.
- It applies the components to the org inside a transaction that has not been committed.
- It compiles Apex and checks references between components.
- It runs the Apex tests required by the chosen test level.
- It evaluates code coverage if the target is production.
- Instead of committing, it rolls back.
The following diagram shows the point where a validation and a real deployment diverge.
The final branch matters for the rest of this tutorial. A passed validation leaves a record on the org: a deployment job with an ID that starts with 0Af. A later chapter shows how quick deploy uses that record to skip the test run.
What a validation does not change
After a validation, whether it passed or failed, the target org has the same metadata it had before. Specifically:
- No new or updated Apex classes, triggers, flows, objects, or fields are saved.
- Components that the package would delete are not deleted.
- Data created by Apex tests is not saved. Tests roll back their own DML (data changes) already, and the validation rollback sits on top of that.
- Existing users see no behavior change. A trigger in the package does not start firing on records.
This is why a validation against production is safe to run during business hours. It is also why many teams run one against production days before a release.
What a validation does check
A validation catches the problems that would otherwise surface during the release window.
| Check | Example of a failure it catches |
|---|---|
| Metadata validity | A field referenced by a page layout is missing from the package and from the target org |
| Dependencies | A class calls a method that exists in the sandbox but not in production |
| Apex compilation | A class uses an API version the org does not support |
| Apex test results | A test asserts on a value that differs because of production configuration |
| Code coverage (production) | The org's overall coverage drops below 75% |
| Org-specific conflicts | A validation rule or required field in production breaks a test that inserts records |
The last two rows are the main reason to validate against the real target. A package that passes in a sandbox can still fail in production because production has different data, configuration, installed packages, and tests.
What a validation does not guarantee
A passed validation says the package would have succeeded at the moment it ran. It says nothing about later.
- The org can change afterward. If someone edits a component in production, or another deployment lands, the validation result may no longer reflect reality.
- Test results can be time-dependent. A test that depends on the current date, on record counts, or on data other users are changing can pass today and fail tomorrow.
- Post-deployment steps are not covered. A validation does not run manual steps such as assigning permission sets, activating a flow version, or loading data. Those need their own checks.
- Behavior that tests do not exercise is not verified. A flow, formula, or page layout with no test is checked for syntax and references, not for correct business behavior.
- It is not free. Running the tests takes the same time as a real deployment would. For a large org,
RunLocalTestscan take an hour or more.
A validation reduces risk. It does not remove it.
Validation and the deployment queue
A validation is a deployment job in the target org, and it uses the same deployment machinery. Two consequences follow.
First, validations and real deployments to the same org compete for processing. If a long validation is running, a real deployment you start may wait for it. For that reason, avoid scheduling a validation close to a real release.
Second, a validation occupies a job record just as a real deployment does. You can see it in Setup > Deployment Status, where it appears with the status In Progress, Succeeded, or Failed. The Setup page lists validations along with real deployments, so check the type of each entry.
Running a validation: a worked example
Suppose you want to release an Apex class AccountService and its test AccountServiceTest to production. First, list both in a manifest, so the validation and the later deployment point at the same components.
<?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>Then run the validation against the authorized org. The alias prod is whatever alias you chose when you authorized the org.
sf project deploy validate \
--manifest manifest/package.xml \
--test-level RunLocalTests \
--target-org prodThe command uploads the components, waits for the org to process them, and prints the outcome. It also prints the job ID. Copy it. If you lose it, you can look it up again in Setup, but writing it down at the time saves a search later.
If the command stops waiting before the job finishes (for example, a timeout on a long test run), the job keeps running on the org. Check it with the report command.
sf project deploy report --job-id 0AfXXXXXXXXXXXXXXX --target-org prodThe 0Af prefix identifies the record as a deployment job. Replace the placeholder with your own ID.
Later chapters cover how to read the full results and how to use this ID for a quick deploy. Here the point is simpler: a validation produces the ID that ties the later release to the tests you already ran.
Validation versus a dry run
You may see sf project deploy start --dry-run in older scripts or documentation. It also performs a check-only deployment: the components are checked and tests run, then nothing is saved. The dedicated sf project deploy validate command is the one to use for this tutorial's workflow, because it is built for the validate-then-quick-deploy flow and reports the job ID that quick deploy needs. When you read other people's scripts, treat the two as related but not interchangeable, and check the current CLI documentation for the version you use.
Common mistakes
- Validating against a sandbox only. A sandbox pass is useful, but it does not prove production will accept the package. Production has its own data, tests, and coverage numbers.
- Validating with a different package than you plan to deploy. If the release manifest differs from the validated one, the validation tells you little. Keep one manifest and use it for both.
- Choosing
NoTestRunand expecting a quick deploy. A validation with no tests has nothing for quick deploy to skip. Pick the test level that the production deployment would need. - Ignoring a failed test because it "usually" fails. A flaky test blocks the release as surely as a broken one. Fix or quarantine it before the release window.
- Assuming the validation is permanent. The result ages. Later chapters give the exact eligibility window. For now, treat a validation as something you refresh when the package or the target org changes.
Summary of the model
- A validation is a real deployment that is rolled back, so the target org's metadata does not change.
- It checks components, dependencies, compilation, tests, and (in production) code coverage.
- It does not run manual steps or prove behavior that no test exercises.
- It does not stay true forever, because the org and the data around it keep changing.
- A passed validation produces a job ID (prefix
0Af) that the release process uses later.
Exercise
A teammate says, "I ran a validation against production, so the new trigger is already live but inactive." Is that correct? Explain what the state of production is after the validation.
Show answer
No. A validation rolls back at the end, so the trigger is not saved in production at all. The org's metadata is the same as before the validation. All that remains is a deployment job record with a job ID starting with 0Af.
Exercise
Your package validated successfully on Monday. On Wednesday an administrator changes a validation rule in production that one of your Apex tests depends on. Does Monday's result still prove the deployment will succeed? Name one other thing a validation never covers.
Show answer
No. A validation only reflects the org at the moment it ran. The changed rule could make the test fail, so you should validate again. Other things a validation does not cover include manual post-deployment steps (such as assigning permission sets or activating a flow) and any behavior that no test exercises.