Sub topic 2
Reading the Deployment Report and Test Failures
A deployment report is the first thing you read when a release goes wrong. It tells you whether Salesforce changed your org, which components caused errors, and which tests failed. Every rollback decision later in this tutorial starts from it.
This chapter assumes you can run sf project deploy start, you know what a package.xml manifest is, and you have seen Apex tests run. It also assumes the previous chapter's point that a failed deployment is all-or-nothing.
Where the report lives
You can read the same deployment result in three places:
- Terminal output from
sf project deploy start, if you waited for it to finish. sf project deploy report, which fetches the result by job ID at any time.- Setup > Deployment Status, which lists in-progress and recent deployments in the org. Each row opens a detail page.
The job ID is the 15 or 18 character ID that starts with 0Af. The CLI prints it when you start a deployment. If you lost it, find it on the Deployment Status page, or ask the CLI for the most recent deployment:
sf project deploy report --use-most-recent --target-org prodUse the job ID when the team has several releases in flight. --use-most-recent picks the last deployment started from your machine, which may not be the one you mean.
sf project deploy report --job-id 0AfXXXXXXXXXXXXXXX --target-org prodThe report holds for a limited time (Salesforce keeps deployment history for about 30 days), so copy the output into your release ticket if you need an audit trail.
The parts of a report
A report has four parts. Read them in this order.
- Overall status. Did the deployment finish, and how?
- Component results. Which metadata components were deployed, and which failed?
- Test results. Which tests ran, which failed, and what was the code coverage?
- Counts and timing. How many components and tests were processed, and when it started and ended.
Overall status
The status values you will meet are:
| Status | Meaning | Org changed? |
|---|---|---|
| Pending | Queued, waiting for a slot | No |
| InProgress | Components or tests are processing | Not yet committed |
| Succeeded | Everything deployed and all checks passed | Yes |
| SucceededPartial | Some components deployed, some did not (only with `rollbackOnError` set to false) | Yes, partly |
| Failed | An error stopped the deployment | No (everything rolled back) |
| Canceling | A cancel request was received | Not yet decided |
| Canceled | The deployment was canceled | No |
Two details matter for rollback planning:
- A Failed deployment to a production org rolls back automatically. The default behavior is that one component error undoes the whole deployment.
- Succeeded only means Salesforce accepted the metadata and the required tests passed. It says nothing about whether your release is correct for users. That is the "bad release" case, covered in the next chapter.
SucceededPartial is rare. It appears when a deployment is run with rollbackOnError turned off, which is only allowed for sandboxes and developer orgs, not production. If you see it, treat the org as being in an unknown mixed state and compare it against your source.
Component results
Salesforce deploys components in dependency order and validates all of them before it commits. Component failures are listed with:
- the component type and full name (for example,
ApexClassandOrderService), - the file path,
- a problem type, usually
ErrororWarning, - a line and column when the problem is in code,
- a message.
A typical failure in the terminal looks like this:
=== Component Failures [2]
Type Name Problem
ApexClass OrderService Variable does not exist: discountRate (3:21)
CustomField Account.Tier__c Field Tier__c has an invalid picklist valueRead the problem text carefully. Most messages fall into a few groups:
- Missing dependency. The component references something that is not in the org or in the deployment, such as a field, class, or record type. The fix is to add the missing component to the manifest, or deploy the dependency first.
- Compile error. Apex or a formula does not compile against the target org's metadata. The target org may differ from your source org.
- Not allowed in this state. For example, changing a field type when the field is used elsewhere, or deleting a component that another component references.
- Permission or feature missing. The target org does not have the feature or license the component needs.
One root cause often produces several errors. If OrderService fails to compile, every class that calls it may also fail. Fix the first error in dependency order, not the longest list.
Test results
When tests run (see the next section), the report adds a test section. Salesforce lists each failure with the class name, the method name, the message, and a stack trace:
=== Test Failures [1]
Name Method Name Message Stacktrace
OrderServiceTest testDiscountApplied System.AssertException: Assertion Class.OrderServiceTest.testDiscountApplied: line 42, column 1
Failed: Expected: 10, Actual: 0Read the stack trace from the top. The first line names where the test failed. Two cases need different recovery:
- The failing line is in the test class (an assertion or setup problem). Your test may be wrong or may expose a real bug.
- The failing line is in code under test, with the test method deeper in the trace. The production code has a defect.
Tests can also fail for reasons unrelated to your release. Common examples are data that exists in production but not in your sandbox, hard-coded record IDs, tests that depend on the current date, and row-lock errors from other jobs running in the org. If a test passes in the sandbox but fails in production, look at those causes first.
Code coverage
Production deployments that include Apex must meet two code coverage rules:
- At least 75% coverage across the org's Apex code.
- Every trigger in the deployment must have some coverage (more than 0%).
The report lists coverage warnings for classes and triggers below the limit. A coverage failure looks like a test failure in the status, but nothing is wrong with behavior. The tests simply do not run enough of the code. A rollback is not the right response. Add tests or deploy the tests that were left out of the manifest.
Which tests ran
The --test-level option controls which tests run, and so which failures can appear:
| Test level | Tests that run |
|---|---|
| NoTestRun | None. Not allowed for production deployments that include Apex. |
| RunSpecifiedTests | Only the test classes you name. |
| RunLocalTests | All tests in the org except those from managed packages. |
| RunAllTestsInOrg | Every test, including managed packages. |
The report only describes tests that ran. If a release passes with RunSpecifiedTests and later breaks something outside the named tests, the report was never going to catch it. When you read a clean report, check the test level that produced it.
Getting the report in machine-readable form
For long failure lists, add --json and filter it. The paths below follow the Metadata API result structure, and exact fields can differ slightly between CLI versions, so inspect the full output once before relying on a filter in automation.
sf project deploy report --job-id 0AfXXXXXXXXXXXXXXX --target-org prod --json > deploy-report.json
jq '.result.details.componentFailures' deploy-report.json
jq '.result.details.runTestResult.failures' deploy-report.jsonYou can also write JUnit XML and coverage files for your CI system:
sf project deploy report --job-id 0AfXXXXXXXXXXXXXXX --target-org prod --junit --results-dir deploy-resultsKeep the JSON file with the release record. It is the evidence you will need if you must explain later what state the org was in.
A reading routine
Use the same order every time, so that you do not skip steps under pressure.
In words:
- Confirm the status. If it is
Failed, the org is unchanged, so there is nothing to roll back. - Read component failures first. They block everything else.
- Read test failures next. Group them by class and message, and find the shared cause.
- Check coverage warnings last. They are the easiest to fix.
- Record what you found, then fix and validate again.
Common mistakes
- Reading only the summary line. "Failed, 3 errors" does not tell you which ones are real. Open the detail.
- Fixing the last error in the list. Errors are not sorted by root cause. Start from the component that others depend on.
- Assuming a failed deployment needs a rollback. It does not. Salesforce rolled it back for you. Start a rollback only when the status is
Succeededand you want the changes gone. - Trusting a pass at a narrow test level. A pass with
RunSpecifiedTestsproves only that those tests passed. - Losing the job ID. Save it in the release ticket when the deployment starts.
Putting the report to work
Suppose you deployed a release to production and the terminal shows Failed with one failing test, OrderServiceTest.testDiscountApplied. The stack trace points to line 42 of the test class, and the assertion says the discount is 0 instead of 10.
Your reasoning should go like this:
- The status is
Failed, so production still has the old code. No rollback is needed. - There are no component failures, so the metadata itself deployed cleanly in validation.
- One test fails on an assertion. Check whether the production org has configuration the sandbox lacks. Perhaps a custom metadata record that sets the discount rate exists only in the sandbox.
- Fix the cause, validate again, and deploy.
Now suppose the status is Succeeded, but support reports that discounts have stopped applying. The report cannot help you here. It shows a clean deployment. This is the point where you stop reading the report and move to choosing a recovery path, which is the next chapter.
Exercise
A production deployment ends with status Failed. The report shows one component failure (a compile error in an Apex class) and no test failures. A teammate proposes redeploying last week's version from Git "to roll back". What do you tell them, and what do you do instead?
Show answer
No rollback is needed. A failed production deployment is rolled back automatically by Salesforce, so the org still has the previous metadata. Redeploying last week's version would only make a second change. Read the component failure, find the cause of the compile error (often a missing dependency or a difference between the sandbox and production), fix it, run a validation with sf project deploy validate, and deploy again.
Exercise
A release passes with --test-level RunSpecifiedTests naming three test classes. Two days later another feature breaks. Why did the deployment report not warn you, and what test level would have been more likely to catch it?
Show answer
The report only covers tests that actually ran. With RunSpecifiedTests, tests outside the three named classes never executed, so a break elsewhere could not appear. RunLocalTests runs all non-managed-package tests in the org and is more likely to catch it, though it still cannot detect defects that no test covers.