Sub topic 7
Building a feature from branch to pull request
This chapter walks one change through the flow: branch, scratch org, build, retrieve, commit, push. It assumes you can use Git branches and pull requests. It also assumes you have the Salesforce CLI (sf) installed, a Dev Hub org enabled, and a Salesforce DX project laid out as in the earlier chapter on project structure.
The shape of the flow
Each feature gets its own branch and its own org. The branch holds the code. The org is where you build and test. Neither one replaces the other.
Step 1: Start from a current main
Branch from an up-to-date main. If your branch starts from stale code, you inherit conflicts before you write anything.
Step 2: Create the branch and the org
git switch main
git pull --ff-only origin main
git switch -c feature/case-routing-rules
sf org create scratch --definition-file config/project-scratch-def.json --alias case-routing --duration-days 7
sf project deploy start --target-org case-routingThe --ff-only flag makes the pull fail instead of creating a merge commit. That failure tells you your local main has diverged, and you should fix that before branching.
The alias case-routing matches the branch name. Matching names make it easy to tell which org belongs to which branch. The 7-day duration keeps the org short-lived, like the branch.
The deploy pushes the current state of main into the empty scratch org. Your work then starts from the same baseline everyone else has.
Step 3: Build and retrieve
Build in the org with Setup, Flow Builder, or by editing Apex locally and deploying. Anything you build with clicks exists only in the org until you retrieve it.
sf project retrieve start --target-org case-routingScratch orgs have source tracking, so this command retrieves only what changed in the org. In a sandbox without source tracking, name what you want with --metadata or --source-dir. Otherwise you may not get your change.
Run git status after retrieving. Check that only files related to your feature changed.
Step 4: Commit and push
git add force-app
git commit -m "Add case routing rules"
git push -u origin feature/case-routing-rulesThen open a pull request into main on your Git host. The validation check from the later chapter runs on it.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| The change is in the org but missing from `git status` | You did not retrieve, or retrieved the wrong scope | Run the retrieve again, naming the metadata if the org has no tracking |
| The diff has hundreds of changed files | Retrieve brought in reordered or unrelated metadata | Discard unrelated files with `git restore` before committing |
| `git pull --ff-only` fails | Local `main` has commits that are not on the remote | Find and resolve the divergence before branching |
| Deploy fails in the new scratch org | The scratch definition file lacks a feature or setting that `main` needs | Update `config/project-scratch-def.json` |
Checking before you open the pull request
- Run
git diff main --statand confirm the files match the feature. - Run your Apex tests in the scratch org.
- Write a pull request description that says what changed and which metadata types are involved.
Exercise
You built a flow in your scratch org, committed, and pushed, but the pull request does not contain the flow. What most likely went wrong, and how do you fix it?
Show answer
You did not retrieve the flow from the org into your local project, so it was never in the files you committed. Run sf project retrieve start --target-org case-routing (naming the flow with --metadata Flow:Name if the org has no source tracking), check git status, then commit and push again.