Sub topic 9
Validating before you deploy
This chapter assumes you can find Setup pages, know the difference between a sandbox and production, and have already uploaded an outbound change set. You also need the Deploy Change Sets permission in the target org. Here you will learn what validation does, how to run it, how to read the result, and how validation relates to the later deploy.
What validation does
Validation is a dry run of a deployment. Salesforce checks the inbound change set against the target org exactly as a real deployment would. It saves nothing. No component is created or changed, and the org stays as it was.
A validation does these things:
- Confirms that every component in the change set can be saved in the target org.
- Checks that dependent components exist, either in the change set or already in the target.
- Compiles Apex classes and triggers.
- Runs Apex tests, according to the test level you choose.
- Reports errors and warnings in the same format as a real deployment.
Validation is optional. You can deploy straight away. In production, though, a failed deployment can take time to sort out, and validation lets you find those failures before you commit to a release window.
Where validation fits
The diagram shows the usual path from an uploaded change set to a finished deployment.
Deployments are all-or-nothing. If one component fails, Salesforce saves none of them. This is true for validation, which saves nothing anyway, and for a real deployment.
Running a validation
- In the target org, open Setup and enter
Inbound Change Setsin Quick Find. - Under Change Sets Awaiting Deployment, find the change set you uploaded. It appears after upload and can take a short time to show.
- Click the change set name to open its detail page. Check the component list and the uploader's name.
- Click Validate.
- Choose a test level if the org offers the choice (see below), then confirm.
- Watch the progress. The Deployment Status page shows the current state, and Salesforce can email you the result.
The result of a validation is recorded in the Validation History section of the change set detail page. It shows the date, the user, the test level, and whether it succeeded.
Choosing a test level
The test level controls which Apex tests run. It has a large effect on how long validation takes and on whether a later deployment can reuse it.
| Test level | What runs | Notes |
|---|---|---|
| Default | In production, local tests run if the change set contains Apex classes or triggers. In a sandbox, no tests run. | The setting you get if you do nothing special. |
| Run Local Tests | All tests in your org except those from installed managed packages. | Common choice for production releases. |
| Run All Tests | Every test in the org, including managed package tests. | Slowest. Rarely needed. |
| Run Specified Tests | Only the test classes you name. | Available only when the change set contains Apex. Each deployed class and trigger must be covered adequately by the named tests. |
The coverage rule for production is that the Apex deployed must have at least 75% code coverage overall, and every trigger needs some coverage. Tests that fail also block the deployment. A validation with the right test level is the best way to find out whether your release meets these rules.
Reading the result
Open Deployment Status from Setup (search for Deployment Status). Validations and deployments both appear here, in the Failed, In Progress and Succeeded sections.
For a failed validation, click View Details. The page lists the following:
- Component failures: the component name, its type, the problem, and sometimes a line and column for Apex.
- Test failures: the test class, method, error message, and a stack trace.
- Code coverage warnings: classes or triggers below the required coverage.
Common failure messages and their usual causes:
| Message pattern | Usual cause | Fix |
|---|---|---|
| `Invalid cross reference id` or `no such column` | A dependent field, object or record type is missing in the target. | Add the dependent component to the source change set and upload again. |
| `Average test coverage across all Apex Classes and Triggers is 62%, at least 75% test coverage is required` | Not enough tests for the deployed code. | Add tests in the source org, include them in the change set, and upload again. |
| Test method failure with `System.AssertException` | The test depends on data or configuration that differs in the target. | Make the test create its own data, or fix the configuration difference. |
| `Insufficient access rights on cross-reference id` | The deploying user lacks permission on a referenced item. | Grant access, or deploy as a user with the right permissions. |
For a deeper look at fixing these, see the troubleshooting chapter later in this tutorial.
Fixing a failed validation
A change set cannot be edited after you upload it. Fixes happen in the source org.
- Note each error from the Deployment Status details.
- In the source org, correct the cause. Often this means adding a missing dependent component to the change set. In some cases you must change the target org instead, for example by enabling a feature.
- If the outbound change set is unchanged, you cannot re-upload it. Clone it, which lets you add or remove components, then upload the clone.
- Upload to the target org again and validate the new inbound change set.
Cloning keeps the component list, so you do not rebuild it. It is a normal part of the workflow.
Quick deployment after a successful validation
In production, a successful validation that ran tests can be turned into a quick deployment. A quick deployment skips the tests, because they just passed, and so finishes much faster.
Conditions apply:
- The validation must have run tests, which means the test level was not a no-tests level.
- It must have succeeded within the last 10 days.
- Tests must have met the coverage requirement.
After a validation succeeds, the change set page and Deployment Status show a Quick Deploy option. Click it to start the deployment.
If the target org changes in a way that matters, such as a new failing test, Salesforce can still reject the deployment. Run it in a quiet period when other administrators are not editing the same components.
A practical validation routine
Use this routine for a production release.
- Validate the change set in a sandbox first when the org relationship allows it. Errors in dependencies show up there at no risk.
- Upload to production in advance of the release window.
- Validate in production with Run Local Tests, or Run Specified Tests when the release contains Apex and a full local run is too slow.
- Fix and re-upload if needed until validation succeeds.
- During the window, use Quick Deploy.
- Confirm the result on Deployment Status, then do any manual steps.
Edge cases
- Validation passes but deployment fails. The target org changed in between, or a test depends on timing or data. Re-validate right before you deploy.
- Long-running tests. Large orgs can take hours to run all local tests. Run Specified Tests shortens this. Schedule the work, and avoid starting another deployment at the same time, since deployments to one org are queued.
- Locked components. Salesforce can lock Apex classes while a deployment runs. Other validations or edits touching them may wait or fail.
- Validation history is per change set. Cloning creates a new change set, so earlier validations do not carry over. Quick Deploy applies only to the validated change set.
- Expired validation. After 10 days, the Quick Deploy option disappears. Validate again.
Exercise
A change set contains one Apex class and its test class. You validate it in production with Run Local Tests and it succeeds on Monday. The release window is the following Friday. What do you do on Friday, and what two things could stop that from working?
Show answer
Use Quick Deploy on the successful validation, which skips running tests again and finishes faster. It can fail to be available or to succeed if more than 10 days have passed since the validation (not the case here, since it is 4 days), if the validation did not run tests, or if the target org changed in between, for example a new failing test or an edited dependent component. If Quick Deploy is unavailable or fails, validate again and then deploy.
Exercise
A validation fails with "Invalid cross reference id" on a page layout. You cannot edit the uploaded change set. What are the steps to fix it?
Show answer
In the source org, clone the change set, add the missing dependent component (for example the custom field the layout references), and upload the clone to the target org. Then open the new inbound change set and validate it again.