Sub topic 1
What scratch orgs are and why teams use them
Prerequisites
This chapter assumes you have the following:
- A Salesforce production, Developer Edition, or trial org where you are a system administrator.
- Basic familiarity with Salesforce orgs and Setup.
- Comfort running commands in a terminal.
- The Salesforce CLI (
sf) installed. - A basic understanding of JSON.
You will not run any commands in this chapter. It sets up the ideas that the next chapters put into practice.
What a scratch org is
A scratch org is a temporary Salesforce org that you create from a configuration file and throw away when you finish. It is empty of your data and customizations until you push your source code into it. You create it on demand with the Salesforce CLI, use it for a bounded task, and delete it or let it expire.
Three properties define a scratch org:
- Source-driven. The org's contents come from your project files in version control, not from clicks made in a shared org.
- Configurable. A definition file states the edition, features, and settings the org should have.
- Disposable. Every scratch org has an expiry date. The default is 7 days, and the maximum is 30.
Why teams use them
Teams that share a sandbox often overwrite each other's work. Nobody can say for certain which changes are in the org or why. Scratch orgs address this in several ways.
| Problem with shared orgs | How a scratch org helps |
|---|---|
| Developers overwrite each other's changes | Each developer or branch gets a private org |
| The org's state drifts from what is in version control | The org is rebuilt from source and the definition file |
| Testing needs a clean starting point | A new org starts without leftover data or metadata |
| Features differ between environments | The definition file sets the edition, features, and settings |
| Automated tests need an environment | A CI job can create an org, run tests, and delete it |
Because the definition file lives in your repository, any teammate can rebuild the same environment. This makes bugs easier to reproduce and reviews easier to run.
The lifecycle at a glance
The usual flow has four steps: define, create, work, discard. A Dev Hub org, which later chapters cover in detail, creates and tracks every scratch org. You authorize it once and then create scratch orgs from it.
Step E matters. Changes you make in the org through Setup do not reach version control by themselves. You must retrieve them into your project before the org is gone.
What scratch orgs are not
Scratch orgs are not copies of production. They do not hold your production data or all of your production metadata. They are also not long-lived. Do not use one as a shared test environment that people depend on for weeks. For that, use a sandbox.
Their limits also apply per Dev Hub. You can only have so many active scratch orgs at once, and only so many can be created per day. Later chapters show how to check those limits.
What comes next
The next chapter explains the Dev Hub org, which owns your scratch orgs. After that, you will enable it, authorize it from the CLI, and create your first scratch org.
Exercise
A teammate says, "I'll just configure everything in my scratch org through Setup and keep using it for a month." Give two reasons this plan conflicts with how scratch orgs are meant to be used.
Show answer
First, scratch orgs expire, and the maximum duration is 30 days, so the org and any changes not saved to source will be lost. Second, configuration done only in Setup is not in version control, so teammates cannot rebuild the org. The changes should be retrieved into the project, and the org should be recreated from source when needed.