Sub topic 9
Validating before you deploy
This chapter assumes you can build an outbound change set and upload it, and that you know what an inbound change set is. You need the Deploy Change Sets permission in the target org.
What validation does
Validation runs a deployment without saving anything. Salesforce checks every component against the target org and runs Apex tests as if it were deploying. If the check passes, the target org stays unchanged. If it fails, you get the same errors a real deployment would give you.
Validate first for these reasons:
- A failed deployment can take a long time to run, especially with many tests.
- Errors appear before anything reaches production.
- A recent successful validation can be used for a quick deploy, which skips the tests (see the callout below).
Start a validation
- In the target org, open Setup and search for Inbound Change Sets.
- Under Change Sets Awaiting Deployment, click the name of the change set.
- Review the component list. Compare it with what you built in the source org.
- Click Validate.
- Choose a test level. The next chapter covers the options. For a production org, the default runs local tests when the change set contains Apex.
- Click Validate again to confirm.
The change set then shows a status in Deployment Status under Setup. You can leave the page. The validation continues in the background.
Read the result
Open Deployment Status in Setup. The Failed and Succeeded sections list each attempt. Click View Details on a validation to see:
- Component failures, such as a missing field that a flow uses.
- Test failures, with the class name, method name, and error message.
- Code coverage warnings, if a class falls below the required threshold.
Production deployments that include Apex need at least 75% overall coverage. Each trigger must also have some coverage.
Fix and validate again
A change set cannot be edited after you upload it. You have two options when validation fails:
- If the problem is in the target org, such as a missing feature or a test that depends on data, fix it there and validate again.
- If the problem is in the change set, go to the source org. Clone the change set, add the missing components, and upload the clone. Then validate the new inbound change set.
Quick deploy
A successful validation with tests can be reused. After it passes, a Quick Deploy button appears next to the change set in Deployment Status. Quick deploy skips the tests, so it finishes faster.
Salesforce keeps this option available for 10 days. It is only offered if the validation ran tests. You need to meet the code coverage requirement.
Check what you are validating
You can compare the change set with the source org by retrieving the same components with the Salesforce CLI. The manifest lists components by type and name, like a change set does.
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>Invoice__c</members>
<name>CustomObject</name>
</types>
<types>
<members>InvoiceService</members>
<members>InvoiceServiceTest</members>
<name>ApexClass</name>
</types>
<version>61.0</version>
</Package>sf project retrieve start --manifest manifest/package.xml --target-org MySandboxCommon validation mistakes
| Mistake | Effect | Fix |
|---|---|---|
| Skipping validation | Errors appear during the real deployment | Validate first |
| Ignoring warnings | Coverage problems surface late | Read all warnings |
| Editing the target org after validating | Result is out of date | Validate again |
| Forgetting a test class | Coverage fails | Add the test class to the change set |
Exercise
A validation of your change set succeeded, but a colleague then deployed another change set to the same production org. What should you do before you deploy yours, and why?
Show answer
Validate again. The earlier result reflects the org as it was at that time. The other deployment may have changed components your change set depends on, so a new validation shows whether it still succeeds.
Exercise
Validation fails because a flow in your change set refers to a custom field that is not in the change set. How do you fix it?
Show answer
Clone the change set in the source org, add the custom field, upload the clone, and validate the new inbound change set. You cannot edit the original change set after upload.