Sub topic 3
Mapping orgs to the places code lives
This chapter assumes you know basic Git (branch, merge, pull request), have used Salesforce metadata such as Apex classes and flows, and can run commands in a terminal with the Salesforce CLI (sf) installed. It does not yet cover the branching model itself. The goal here is to decide what each kind of org is for, so that later chapters can say where a change goes without ambiguity.
Orgs are places to run code, not places to keep it
In many Salesforce teams, the org is where the "real" version of a feature lives. Someone builds a flow in a sandbox, someone else edits a validation rule in production, and the team treats whichever org was touched last as the truth. This makes branching strategy meaningless, because Git holds a copy that nobody trusts.
The rule used in the rest of this tutorial is the reverse:
- Git holds the source of truth for everything the team owns.
- Orgs are targets. Each one receives a version of the source for a specific purpose.
- Changes flow from Git to orgs. The exceptions, such as retrieving declarative work from a scratch org, always end with the change committed to Git.
Every org type below is judged on one question: what kind of Git state should it hold, and for how long?
The org types you will use
| Org type | Created from | Typical life | Data | Best used for |
|---|---|---|---|---|
| Scratch org | A definition file in your repo | Hours to 30 days (default 7) | None, or seeded by a script | One feature branch |
| Developer sandbox | A copy of production metadata | Weeks to months | None | Personal or shared development when scratch orgs are not an option |
| Developer Pro sandbox | A copy of production metadata | Weeks to months | None, more storage | Heavier development or integration work |
| Partial Copy sandbox | A copy of production metadata plus a data sample | Refreshed on a schedule | Sample | Integration and UAT testing |
| Full sandbox | A full copy of production | Refreshed on a schedule | Everything | Performance testing, final UAT, staging |
| Production | n/a | Permanent | Real | Serving users |
Refresh intervals and storage limits depend on edition and licence. Check your own org's limits before you plan around them.
Scratch orgs
A scratch org is a temporary org built from a definition file. Because the definition file lives in Git, every developer starts from the same baseline. A scratch org expires after at most 30 days, so it cannot become anyone's long-term home for work.
{
"orgName": "Case Routing Dev",
"edition": "Developer",
"features": [],
"settings": {
"lightningExperienceSettings": {
"enableS1DesktopEnabled": true
}
}
}Save this as config/project-scratch-def.json. Create an org from it with:
sf org create scratch --definition-file config/project-scratch-def.json --alias case-routing --duration-days 7Scratch orgs hold no production data and start empty. Anything the feature needs, such as record types, test users, or sample accounts, has to be in source or in a seed script. That is a useful constraint, because it forces setup knowledge into the repository.
Scratch orgs also have limits. They are created from a Dev Hub org, and each Dev Hub caps how many can be active and how many can be created per day. If your features depend on a licence or a managed package that scratch orgs cannot reproduce, you may need a sandbox instead.
Developer and Developer Pro sandboxes
A Developer sandbox is a copy of production's metadata with no data. It is the fallback when scratch orgs do not fit. Typical reasons are a very large existing metadata footprint, a dependency on a feature that scratch orgs cannot enable, or a team that has not yet adopted scratch org definitions.
You can create one from the CLI against production:
{
"sandboxName": "dev1",
"licenseType": "Developer",
"description": "Case routing development"
}sf org create sandbox --definition-file config/sandbox-def.json --alias dev-sbx --target-org prodThe danger is longevity. A sandbox that lives for months drifts away from main, as people deploy to it from different branches or edit it directly. If you use one per developer, treat it like a scratch org. Before starting a new feature, deploy the current main into it, or refresh it. If several developers share one sandbox, expect their metadata changes to collide there before they ever reach Git.
Integration and UAT sandboxes
Partial Copy and Full sandboxes are shared, longer-lived environments. People other than developers use them: testers, business users approving a release, and integration partners. For these, the usual mapping is:
- Integration sandbox: receives
mainafter each merge, so the team can see how merged changes behave together. - UAT sandbox: receives a chosen version of
main(a tag, covered in a later chapter) for business sign-off.
No one should develop directly in a shared environment. A testing org that people also edit stops being a reliable test of what is in Git.
Production
Production receives changes only from Git, through a deployment that passed validation. Direct edits in production do happen, especially during incidents or by administrators who use Setup, but each one creates drift between the org and the repository. The chapter on org drift covers how to find and absorb those edits. For now, the principle is that a change that reaches production without passing through Git is a defect in the process, even when the change itself is correct.
Which Git state each org should match
An org is only useful as a test target if you know which commit it represents. The mapping below is the one the rest of this tutorial uses.
| Org | Should match | Updated by |
|---|---|---|
| Scratch org or personal sandbox | A feature branch | The developer, by deploying from their branch |
| Integration sandbox | Tip of `main` | Automatic deployment after each merge |
| UAT sandbox | A release tag cut from `main` | A deliberate promotion |
| Production | A release tag that passed UAT | A deliberate promotion |
Two consequences follow from this table.
- Orgs lower in the table trail or equal
main. Orgs higher in the table run ahead of it, but only on an unmerged branch. A feature branch should never be deployed into a shared org. - You can answer "what is in UAT?" by looking up a tag, not by opening the org and guessing.
Keeping org identities straight in the CLI
Teams that deploy to several orgs make mistakes when the CLI points at the wrong one. Two habits prevent most of them.
First, give each org a consistent alias. Use short names that describe the role, not the person:
sf org login web --alias prod
sf org login web --alias uat --instance-url https://test.salesforce.com
sf org listSecond, always pass --target-org explicitly in scripts and in commands you copy into pull request descriptions. Relying on a default target set earlier in the session is how a deployment meant for a scratch org lands in a shared sandbox.
sf project deploy start --source-dir force-app --target-org case-routingNever store credentials for production or shared orgs in the repository. Authentication belongs in the CI system's secret store or in each developer's local CLI configuration.
Common mistakes
- Treating a long-lived sandbox as the source of truth. If features are only complete when they are in a particular sandbox, Git is not authoritative. Fix it by deploying from Git and retiring the habit of building ahead of the repository.
- Sharing one developer sandbox among several feature branches. Branches deployed in turn overwrite each other, and a test that passes in the sandbox may pass only because of leftover metadata from another branch. Give each branch its own org, or deploy only one at a time.
- Skipping the baseline. A scratch org with no
maindeployed lacks the dependencies the feature needs. Deploymainfirst, then your branch. - Letting scratch orgs outlive their branch. Once the pull request merges, delete the scratch org. Its contents are now in
main, and a stale copy only invites confusion. - Using an org's current state as evidence of what was released. Orgs can be edited. Tags cannot, in practice, be moved without a visible force push.
Summary
Git is the source of truth, and orgs are disposable or promoted targets. A scratch org or developer sandbox matches a feature branch. An integration sandbox follows main. UAT and production receive tagged versions of main. The next chapter uses this mapping to compare branching models.
Exercise
A tester reports a bug in the UAT sandbox. A developer opens UAT, finds the cause, edits the Apex class directly in the org, and the bug disappears. According to the mapping in this chapter, what has gone wrong, and what should the developer do next?
Show answer
UAT is supposed to match a release tag cut from main, and the direct edit means it no longer does. The fix exists only in the org, so Git does not contain it and the next promotion will overwrite it. The developer should retrieve the change, or reproduce it on a new feature branch from main in a scratch org or developer sandbox. They should then open a pull request, merge it, and promote a new tag to UAT.
Exercise
Your team has three developers, each working on a separate feature, and one developer sandbox. Name one risk of deploying each branch to that sandbox in turn, and one way to avoid it.
Show answer
Metadata from a previously deployed branch stays in the sandbox, so a test can pass because of leftover components that are not in the branch under test. Avoid it by giving each developer their own scratch org or sandbox, or by resetting or refreshing the shared sandbox to match main before each branch is deployed.