The independent daily for Salesforce admins, developers and architects

Curated by Abhinav (opens in a new tab)

Must-read

When Salesforce runs Apex tests on a deployment, and how to run fewer (opens in a new tab)

You will learn which test levels Salesforce applies by default, the coverage rules that constrain RunSpecifiedTests, and five practical ways to shorten production deployments.

Why read this

The post also flags that one Flow-related default is not yet verified in production.

  1. Check whether the package contains Apex or Flows
  2. Pick a test level or accept the default
  3. Apply the 75% coverage rule
  4. Quick deploy a validated package
How Salesforce decides which Apex tests to run on a production deployment

Apex tests are usually the slowest part of a Salesforce deployment to production. The article lists the test levels the Salesforce CLI offers: NoTestRun, RunSpecifiedTests, RunLocalTests, RunAllTestsInOrg and RunRelevantTests, the last still in beta. When no level is chosen, the default depends on the target: sandboxes and Developer Edition orgs run no tests, while production runs all local tests if the package contains Apex classes or triggers. The author notes that Flows set to activate on deployment also count, which is a common cause of unexpectedly slow deployments.

The coverage rules decide whether a production deployment can pass. With RunLocalTests or RunAllTestsInOrg, the org needs at least 75% Apex coverage overall and every test that runs must pass. With RunSpecifiedTests, each Apex class and trigger in the package needs 75% coverage on its own from the named tests, so a class with no selected test fails even when a full run would have passed.

The article gives five ways to run fewer tests: keep unchanged Apex out of the package, deploy only the differences from the target, specify tests while checking per-class coverage, consider RunRelevantTests with its beta limits in mind, and validate ahead then quick deploy within 10 days. It also suggests shipping configuration separately from code, since a package with no Apex needs no tests at the default level. The author tested check-only deployments against a Developer Edition org only, so the Flow default has not been confirmed in production. The post closes by describing how the Diffbridge tool’s Smart test level automates these choices.

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

Worth reading?

Validate ahead of the release window, then quick deploy without re-running tests (opens in a new tab)

You will learn what conditions a validation must meet to be promoted by quick deploy, including the 10-day limit and coverage rules, and how to structure a release around it.

Why read this

You will also see a CLI caveat about validating packages with no tests.

  1. Validate ahead of the window against the target
  2. Check the validation passed and note its ID
  3. Quick deploy that ID inside the window
  4. Validate again if the package or target changes
The validate-then-quick-deploy workflow for a release window

Production deployments that run Apex tests are slow, and the release window is short. Salesforce lets you run a deployment as a validation, also called a check-only deployment, which runs everything a real deployment would, tests included, but saves nothing in the org. If the validation passes, you can later promote it with a quick deploy, which deploys the validated components without running the Apex tests again. The Metadata API calls this deployRecentValidation(), the Salesforce UI offers it on the Deployment Status page, and the CLI uses sf project deploy validate followed by sf project deploy quick.

Salesforce sets several conditions. The components must have validated successfully for the target environment within the last 10 days, and the Apex tests in the target org must have passed as part of that validation. Code coverage must also be met: with all tests or all local tests, overall coverage must be at least 75% and Apex triggers must have some coverage; with RunSpecifiedTests, each deployed class and trigger must be at least 75% covered individually. Salesforce says a validation with passing tests only sometimes qualifies, so the author advises confirming it rather than assuming. Trailhead’s guidance adds that you should not deploy again or make major changes to the org in between.

Two consequences follow. A validation without tests cannot be promoted this way, because the quick deploy skips tests that the validation already ran, so there is nothing to skip. The tests chosen at validation time are the tests you get, and a quick deploy does not add or change them. If the per-class coverage rule fails under RunSpecifiedTests, the quick deploy is unavailable, so it is better to discover that during validation than inside the window. The suggested workflow is to validate ahead of the window against the same target with the test level you will rely on, check that it passed, note its ID, then quick deploy that ID inside the window. If the package or target changes, validate again. The 10-day limit is the main thing to watch, since a validation run before a long weekend may have expired.

The author also reports that sf project deploy validate rejected –test-level NoTestRun in their CLI run. To check a package with no tests, sf project deploy start –dry-run –test-level NoTestRun performs a check-only deployment, but nothing can be quick deployed afterwards. Some Stack Exchange answers report that a CLI –dry-run is not treated as a validation by sf project deploy quick, so use sf project deploy validate when planning a quick deploy and check this against your CLI version.

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

Worth reading?