Sub topic 8
Testing in pre-release and preview scratch orgs
This chapter assumes you know the basics of Salesforce administration, understand how sandboxes differ from production, and can run a command in a terminal. You should also have read the earlier chapters on the release timeline and on choosing preview and non-preview sandboxes. Here you will learn how to get an org running the upcoming release before your own sandboxes and production move to it, and how to use that org without drawing wrong conclusions from it.
Why a separate test org helps
Sandbox preview moves your existing sandboxes onto the new release early. That is useful, but those sandboxes carry years of configuration, data and half-finished work. When a test fails there, you cannot always tell whether the release caused it or your own org did.
A scratch org is a disposable org built from a definition file and your source code. Because you build it from scratch, you control what it contains. If you deploy your metadata to a scratch org on the upcoming release and something breaks, the list of suspects is short.
Two kinds of org are relevant here:
- Pre-release org: a free, short-lived org that Salesforce offers during the preview window. It runs the upcoming release and is mainly meant for exploring new features and testing your apps, including managed packages for partners. You sign up for it through Salesforce's pre-release program.
- Preview scratch org: a scratch org that you create with the Salesforce CLI. You set the
releaseoption in the definition file toPreview, and the org is created on the upcoming release.
The scratch org is the better fit for repeatable, scripted testing. The pre-release org is better for a person clicking through new features.
Where preview scratch orgs fit in the timeline
The preview window is the weeks before the production upgrade. During that window, Salesforce publishes the next release's features in preview instances. Outside that window, the Preview value gives you nothing different from the current release.
This is the first edge case to remember. If you set "release": "Preview" when no preview is available, the org is created on the same release as everything else. Your test would then look like it passed against the new release when it did not run against it at all. Always check the version of the org you created, as shown later in this chapter.
Define the preview scratch org
The definition file describes the org. For release testing, three settings matter most: the edition, the release, and any features or settings your code depends on.
{
"orgName": "Release Preview Org",
"edition": "Developer",
"release": "Preview"
}The release option accepts two values. Preview creates the org on the upcoming release. Previous creates it on the release before the current one, and it is also available only during a limited period around releases. If you leave the option out, the org is created on whatever release the instance currently runs.
A definition file this small is only a starting point. Real projects need more, for example:
- Features: if your code uses a feature that is not on by default, such as multi-currency or Platform Cache, list it in the
featuresarray. Otherwise deployment fails for reasons that have nothing to do with the release. - Settings: if your org relies on a particular setting, add it under
settings. Otherwise the scratch org uses defaults that may differ from your sandboxes. - Edition: a Developer edition scratch org has different limits from an Enterprise one. If your production org is Enterprise, choose
Enterpriseso limits are closer to what you actually run.
Keep one definition file for normal development and a second one for release testing. The second file can be a copy with release added. That way a normal development org is never accidentally created on a preview instance.
Create the org with the Salesforce CLI
The Salesforce CLI command is sf org create scratch. You need a Dev Hub org that is authorized in the CLI first, because scratch orgs are created from a Dev Hub.
sf org create scratch \
--definition-file config/project-scratch-def.json \
--alias preview-org \
--duration-days 7 \
--set-defaultWhat each flag does:
--definition-filepoints to the JSON file that describes the org.--aliasgives the org a short name, so later commands can say--target-org preview-org.--duration-days 7sets the lifetime. A scratch org can live from 1 to 30 days, and it is deleted automatically afterwards. Seven days is long enough for a test cycle and short enough that nobody keeps depending on it.--set-defaultmakes it the default target org for the project.
Use a separate definition file name if you keep two, for example config/release-preview-scratch-def.json, and pass that path instead.
Confirm the org is on the preview release
Do not assume the org is on the preview release. Open the org and check the version, or read it from the CLI:
sf org display --target-org preview-org --verboseThe output includes the instance URL and the API version. Compare the API version with the one in your current production org. During preview, the preview org shows the next API version, which is one higher than production. Salesforce also publishes which instances are on preview in its release information, so you can cross-check the instance name.
If the versions match production, the preview window may not be open yet. Delete the org and try again later.
Deploy your code and configuration
Once the org exists, push your source into it. For a source-tracked scratch org, this is:
sf project deploy start --target-org preview-orgRead the deployment result carefully. A failed deployment on the preview org that succeeds on your current sandbox is already a finding. Typical causes are a changed validation rule on a standard object, a field type that is no longer allowed in a given context, or an Apex class that depends on behavior that changed.
The API version in your project
Your project file sets the API version used when you deploy and retrieve source:
{
"packageDirectories": [
{
"path": "force-app",
"default": true
}
],
"sourceApiVersion": "65.0"
}Keep this value where it is during preview testing. Your goal is to see how your existing metadata behaves on the new platform, not to change two things at once. If you raise sourceApiVersion to the new release at the same time as you deploy to the preview org, you cannot tell whether a failure comes from the new platform or from the new API version. Test first with the current version. Raise it deliberately afterwards, and test again.
The same applies to the API version stored in each Apex class and Lightning component. Those versions are saved with the metadata and control some behavior, so they stay as they are until you change them on purpose.
Load data and run tests
An empty org tests very little. Add a small, representative data set so your flows and Apex have records to work on. Common choices are:
- Data created by an Apex script or a data plan that you keep in source control.
- A few records for each object your key processes touch, including edge cases such as records with missing optional fields.
Then run your tests. Running the local Apex tests gives the first regression signal:
sf apex run test \
--target-org preview-org \
--test-level RunLocalTests \
--result-format human \
--wait 20RunLocalTests runs the tests in your own namespace and skips tests from installed managed packages. --wait 20 makes the command wait up to 20 minutes for results instead of returning immediately.
After the Apex tests, work through the other layers:
- Run any record-triggered or screen flows with test records and compare the results to what you get in the current release.
- Open the key pages and apps your users rely on, and check that components load and layouts render.
- Run integrations that can be pointed at a test endpoint.
Automating these steps in CI is covered in a later chapter. For now, the important point is that the same commands run on the preview org and on a normal org, so you can compare the two.
Interpreting what you see
A failure on the preview org has one of three causes, and the fix is different for each.
| Cause | How to recognize it | What to do |
|---|---|---|
| Release change | Same source passes on a current-release org and fails on the preview org | Find the matching item in the release notes or Release Updates, adjust code, or report an issue to Salesforce |
| Your own defect | Fails on both orgs | Fix it in the normal development process |
| Scratch org setup | Fails because a feature, setting or data item is missing | Fix the definition file or data script and recreate the org |
To tell the first two apart, create a second scratch org from the same definition file with release removed, deploy the same source, and run the same tests. This control org is cheap to make and removes guesswork. Check your Dev Hub limits first, because the number of active scratch orgs is capped.
Limits and gotchas
- Time limited: the preview scratch org exists only while the preview is available, and the org itself is deleted when its duration ends. Do not store anything there that you need later.
- Preview software is not final: Salesforce can still change features between preview and production. A passing test in preview is strong evidence, not a guarantee. Retest in a sandbox once it is upgraded.
- Limits differ from your real org: scratch orgs have their own storage, and Developer edition has lower limits than Enterprise. A limit-related failure may not appear in production, and the reverse can also happen.
- Managed packages: installed packages must support the preview release. If a package version is not available in preview, it will not install, and you will not be able to test things that depend on it.
- Data volume: a scratch org with a few records will not reveal performance changes, such as a slower query or a CPU time limit on a large batch. Use a sandbox with realistic data for that.
- Dev Hub in a different release state: the Dev Hub itself does not need to be on the preview release. Only the created scratch org is.
Clean up when you are done:
sf org delete scratch --target-org preview-org --no-promptPutting it together
A repeatable routine looks like this:
- When the preview window opens, create the preview scratch org from the release definition file.
- Confirm the API version is the upcoming one.
- Deploy source at the current
sourceApiVersion. - Load the test data set.
- Run local Apex tests, then flows and key user journeys.
- For each failure, compare against a control org and classify it with the table above.
- Log findings against the release notes items they relate to.
- Delete the org.
Write the results down, even when everything passes. A short record of what was tested and on which date is useful in the readiness checklist later.
Exercise
Your Apex tests pass in your preview scratch org, but one test fails in a sandbox that was also upgraded to preview. What are two reasons for the difference, and what do you do next?
Show answer
The scratch org is built from source only, so it lacks the sandbox's real data volume, installed packages, user permissions and older configuration. Any of these can cause the sandbox failure. The failure could also be a defect in your own code that the clean data in the scratch org never triggers. Next, run the same test in a sandbox that is not on preview. If it fails there too, it is your own defect. If it passes there, the cause is a release change, so look for the matching entry in the release notes.
Exercise
You create a scratch org with "release": "Preview" in October, before the preview window has opened. The org's API version matches production. What happened, and what should you do?
Show answer
Without an available preview, the org was created on the current release, so it is not testing the upcoming release. Delete it, wait until the preview window opens, create it again, and confirm with sf org display that the API version is one higher than production before relying on any test results.