The independent daily for Salesforce admins, developers and architects

Curated by Abhinav (opens in a new tab)

Must-read

Where the Metadata API deployment result keeps its errors and coverage warnings (opens in a new tab)

You will know which fields of the deployment result hold component errors, test failures and coverage warnings, and why a deployment can fail with an empty componentFailures list.

Why read this

The post also gives a checklist for diagnosing a failed deployment.

  1. Set includeDetails to true
  2. Check componentFailures
  3. Check runTestResult failures
  4. Check runTestResult codeCoverageWarnings
Where to look when a deployment fails with no component errors

A failed Metadata API deployment often reports no component errors because the cause sits in another part of the result. The post explains the result layout, using Salesforce’s documentation. The overall outcome is in the DeployResult returned by checkDeployStatus(), but the detail is in its details field, which is only populated when includeDetails is set to true.

The details hold componentFailures and componentSuccesses, each a list of DeployMessage entries, plus runTestResult and retrieveResult where applicable. Each DeployMessage is typed as Warning or Error, a distinction that did not exist before API version 18.0, when every problem stopped the deployment. While a deployment is running, only componentFailures is filled in; the other fields appear after it finishes, so a mid-run check can show failures with no test results yet.

Test-side problems sit one level further down, in runTestResult. Failing tests are listed in its failures, and coverage problems in codeCoverageWarnings. A deployment can therefore fail with componentFailures empty and no test failing, for example when a class is not covered under a RunSpecifiedTests run. The post closes with a checklist: confirm includeDetails was set, check componentFailures, then check runTestResult failures and coverage warnings, make sure the deployment has finished, and compare the package’s classes with the classes the named tests cover.

The post also notes that the component lists moved from the deployment result into details, a change Salesforce says took effect in API version 29.0, so older scripts may look in the wrong place.

Read the original at dev.to (opens in a new tab)

Worth reading?

Recommended

Why RunSpecifiedTests fails on a class RunLocalTests passes (opens in a new tab)

You will learn how RunLocalTests and RunSpecifiedTests apply different per-class coverage rules, why a specified run can fail at 0% coverage, and where the coverage warning appears in Metadata API results.

RunLocalTests

  • Runs all local tests
  • Overall coverage of at least 75%
  • Coverage counted from all tests that ran

RunSpecifiedTests

  • Runs only the named tests
  • Each class at least 75% individually
  • Coverage counted from named tests only
RunLocalTests and RunSpecifiedTests apply different coverage rules

Salesforce production deployments are usually validated at one of two test levels, RunLocalTests and RunSpecifiedTests. RunLocalTests runs every test in the org except those from installed managed and unlocked packages, and it is the default when the package contains Apex classes or triggers. RunSpecifiedTests runs only the tests named in the runTests option.

The two levels count coverage differently. RunLocalTests requires overall org coverage of at least 75 percent, so a weak class can pass if the rest of the org is well covered. RunSpecifiedTests requires each Apex class and trigger in the package to reach 75 percent individually, and only the named tests count toward that figure. A class covered by none of the named tests therefore fails the deployment even when a full local run would have passed.

The article gives a hypothetical package of 13 components with three Apex classes, where two named tests cover two of them and the third sits at 0 percent. Two fixes are offered: add a test that covers the class, or remove the class from the package if it has not changed. Coverage failures are reported under details.runTestResult.codeCoverageWarnings in the checkDeployStatus() result, which appears only when includeDetails is true, so tooling that prints just the error lists can make such a failure look unexplained.

The author suggests RunLocalTests when the simplest and safest run matters more than time, or when it is unclear which tests cover which classes. RunSpecifiedTests suits teams that know the covering tests for every class and want a shorter run. The piece also notes that RunRelevantTests is in beta and should be checked against current documentation, and that NoTestRun applies only to development environments, not production. The Salesforce CLI exposes these choices through –test-level and –tests. The article closes with a note on the author’s own tool, Diffbridge.

Read the original at dev.to (opens in a new tab)

Worth reading?