Sub topic 14
Troubleshooting failed deployments
This chapter assumes you can build an outbound change set, upload it, and validate and deploy it from the inbound change set page in the target org. It also assumes you know the Apex test levels. The goal here is to read a failure, find its cause, and fix it without guessing.
How a failed deployment behaves
A change set deployment is all or nothing. If any component fails, or any required Apex test fails, Salesforce rolls back the whole deployment. Nothing from the change set stays in the target org. This makes failures safe to retry, but it also means one bad component blocks everything else in the set.
You will see failures in two places:
- Validation. The same checks run as in a real deployment, but nothing is saved. Fix problems here first.
- Deployment. The real run. It can still fail even after a successful validation, because the target org may have changed in between.
Open Setup, search for Deployment Status, and look at the Failed section. Click the change set name to see the details. The detail page has a Component Failures section and, if tests ran, a Test Failures section.
Reading the Component Failures table
Each row shows the component name, its type, and a Problem message. Start with the message text, because it usually names the missing or conflicting item. The table below lists the messages you will meet most often.
| Message pattern | Likely cause | Fix |
|---|---|---|
| `In field: ... - no CustomField named ... found` | A referenced field is missing in the target org. | Add the field to the change set, or deploy it earlier. |
| `Invalid reference` or `no ... named ... found` on a flow, layout, or Apex class | A dependent component was not included. | Use **View/Add Dependencies**, then upload again. |
| `Variable does not exist` or `Invalid type` in Apex | The class depends on a field or object that is missing in the target. | Add the object or field and its permissions. |
| `Cannot modify managed object` | You tried to change a component owned by a managed package. | Remove it from the change set. Match package versions across orgs instead. |
| `This component has been modified in the target org` style conflicts | A component is locked, or settings differ between the orgs. | Check the target org for the same component and compare settings. |
| `Unknown user permission` | A feature behind a permission is not enabled in the target org. | Enable the feature in the target org, then retry. |
| `entity is locked` or `UNABLE_TO_LOCK_ROW` | Another deployment, a running job, or an Apex job holds the component. | Wait, check Deployment Status for in-progress jobs, and retry. |
Missing dependencies
Missing dependencies are the most common cause of failure. A change set does not automatically include everything a component needs. For example, a page layout that shows a custom field fails if the field is not in the target org and not in the change set.
To fix this in the source org:
- Open the outbound change set.
- In Change Set Components, click View/Add Dependencies.
- Select the listed components that do not yet exist in the target org.
- Click Add To Change Set.
- Upload the change set again.
Two points trip people up:
- The dependency list only shows what Salesforce can detect. References in strings, such as a field name built in Apex with
Schema.getGlobalDescribe(), or an object name inside a dynamic SOQL string, do not appear. - A dependency that already exists in the target org does not need to be included. Adding it anyway can overwrite the target version with the source version. Check the target first.
To compare what the target org already has, you can retrieve a manifest list from each org and compare the results. A package.xml lists metadata by type and name, the same kind of list a change set holds.
<?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>The Salesforce CLI retrieves the components named in the manifest from an org. Run it once against the sandbox and once against the target, then compare the files.
sf project retrieve start --manifest manifest/package.xml --target-org MySandboxIf the retrieve fails for a component in the target org but not in the sandbox, that component is missing there. That is the same condition that makes a change set fail.
Failing Apex tests
When a deployment runs tests, the Test Failures section lists the class, the method, the error message, and a stack trace. Read the message first. The stack trace points to the line.
Common causes:
- Tests that depend on org data. A test using
SeeAllData=true, or one that queries existing records, may pass in a full sandbox and fail in production, where the data differs. Create test data inside the test. - New validation rules or required fields. A rule deployed in this change set, or already in production, can reject the records your test inserts. The message often reads
FIELD_CUSTOM_VALIDATION_EXCEPTIONorREQUIRED_FIELD_MISSING. - Automation that differs between orgs. A flow or trigger active in production may fire on test inserts and throw an error that the sandbox never showed.
- Unrelated tests. At the Run all tests level, any failing test in the org blocks the deployment, even if your change set did not touch that code.
Here is a test written to avoid org-data problems. It builds its own record and asserts a result.
@IsTest
private class InvoiceServiceTest {
@IsTest
static void createsInvoiceWithDefaultStatus() {
Invoice__c inv = new Invoice__c(Amount__c = 100);
insert inv;
Invoice__c saved = [SELECT Status__c FROM Invoice__c WHERE Id = :inv.Id];
System.assertEquals('Draft', saved.Status__c);
}
}After you fix a test, remember that a test class is itself a component. If the fix is in the sandbox, add the updated class to the change set and upload again. You cannot edit an uploaded change set, so you clone it or add components to a new one.
Code coverage failures
Production deployments that include Apex need at least 75% overall coverage, and every trigger needs some coverage. A message such as Average test coverage across all Apex Classes and Triggers is 72%, at least 75% test coverage is required means the deployment as a whole is below the threshold. A message about one class means that class has no tests in the run.
Check which test level you chose. With Run local tests, managed package tests are skipped, and coverage is measured across your own code. With Run specified tests, each class or trigger you deploy must reach 75% coverage from the tests you named. Adding the missing test classes to the change set and naming them in the deployment usually fixes this.
Stale validations and quick deploy
A successful validation can be used for a quick deploy, which skips running tests again. A validation stops being usable after 10 days. It also stops being usable if you change the change set or if the target org changes in a way that affects the result. If Quick Deploy is missing, validate again.
Between validation and deployment, other people may deploy into the same target org. Their changes can introduce new conflicts. If a deployment fails right after a passing validation, compare recent deployments in Deployment Status.
Other causes worth checking
- Component limits. A change set has size limits. If the upload fails, split it into smaller sets.
- Permissions. The user needs the Deploy Change Sets permission in the target org. Without it the inbound change set is not usable.
- Object and field permissions. Deploying a field does not grant access to it. Include the profile or permission set, or assign access manually, otherwise tests and users may fail to see it.
- Connection and API version issues. If the target org does not appear in the upload list, check that the deployment connection is authorized in the target org, and that the orgs are related.
- Feature mismatch. The target org may lack a feature that the sandbox has, for example a different edition or a setting disabled. Enable the feature or remove the dependent components.
- Active and inactive flows. A flow that is inactive in the source stays inactive after deployment. A flow that is active in the target and replaced by a new version may fail if its own tests or references fail.
A repeatable approach
- Read the first error message. Later errors are often caused by the first one.
- Decide whether the cause lies in the source org, the target org, or the change set contents.
- Make the smallest fix. Prefer adding a missing component over removing a feature.
- Clone or create a new change set if you changed components, and upload it.
- Validate again before deploying.
- Record what happened, so the next person can follow the same steps.
Exercise
A validation of a change set fails with "no CustomField named Invoice_c.Status_c found" on a page layout. The layout is in the change set, but the field is not. The field already exists in the sandbox. What should you do, and what should you check first in the target org?
Show answer
Open the outbound change set in the sandbox, use View/Add Dependencies, add Invoice_c.Status_c, and upload again (clone the change set if it is already uploaded, since uploaded sets cannot be edited). First check whether the field already exists in the target org. If it does, adding it may overwrite the target version, so compare the two before including it. Then validate again before deploying.
Exercise
A validation passed yesterday. Today the deployment fails with a test failure in a class you did not change. Name two likely reasons and one way to confirm each.
Show answer
One reason is that another deployment or data change in the target org altered the environment, for example a new validation rule or required field. Confirm it by checking Deployment Status for recent deployments and reading the test failure message. A second reason is that the test level is Run all tests, so an unrelated failing test blocks the deployment. Confirm it by checking the test level used and whether the failing class is part of your change set.