Sub topic 2
Shaping a Scratch Org with a Definition File
Assume you know basic Salesforce metadata, Git, and command-line use. This chapter focuses on scratch org configuration, source tracking, and choosing an environment.
Define the Scratch Org
A scratch org is a temporary Salesforce org created from a Dev Hub. Its definition file describes the edition, features, settings, and sample data that the org needs.
Create config/project-scratch-def.json:
{
"orgName": "Acme Development",
"edition": "Developer",
"hasSampleData": false,
"settings": {
"lightningExperienceSettings": {
"enableS1DesktopEnabled": true
}
}
}Create the org and deploy project source:
sf org create scratch --definition-file config/project-scratch-def.json --alias my-scratch --duration-days 7 --set-default
sf project deploy start --target-org my-scratchThe definition file makes org creation repeatable. Store it in Git with the project.
Push, Pull, and Track Changes
Source tracking records differences between the local project and a scratch org.
sf project deploy startsends local metadata to the org.sf project retrieve startbrings metadata from the org into the project.sf project deploy previewshows likely deployment changes before deployment.
Prefer changing files in Git, then deploying them. Retrieve changes only when an intentional configuration change was made in the org. Otherwise, local and org versions can drift.
Lifespan and Limits
Scratch orgs are disposable. Their default lifespan is short, and the maximum duration is limited. They also consume allocation limits in the Dev Hub. Delete unused orgs and recreate them rather than treating them as permanent environments.
Choose the Right Org
| Need | Best fit |
|---|---|
| Repeatable feature development or CI | Scratch org |
| Longer-lived development with configuration stability | Developer sandbox |
| Testing with a subset of production data | Partial Copy sandbox |
| Full production-data testing | Full sandbox |
Use a sandbox when data volume, integration fidelity, or a stable shared URL matters more than fast recreation. Use a scratch org when automation, isolation, and repeatability matter most.
Exercise
A developer needs a clean org for every pull request. Should the team use a scratch org or a Full sandbox, and why?
Show answer
Use a scratch org because it can be created from a definition file, populated from source, and discarded after the test.