Sub topic 2
Which Orgs Support Source Tracking
This chapter assumes you have a Salesforce DX project with sfdx-project.json and a force-app directory. It also assumes the sf CLI is installed and you have authorized at least one org. You should know what metadata components are, such as Apex classes and custom objects.
Tracked and untracked orgs
Source tracking works only against orgs that record changes to metadata. Which commands you run depends on the type of org you target.
| Org type | Source tracking | How you choose what to move |
|---|---|---|
| Scratch org | Always on | Run the command with no source flags |
| Developer sandbox or Developer Pro sandbox | Optional, off by default | Run with no flags if enabled, otherwise name the source |
| Partial Copy or Full sandbox | Not available | Name the source explicitly |
| Production, Developer Edition, trial orgs | Not available | Name the source explicitly |
The sandbox row needs a caveat. Tracking for Developer and Developer Pro sandboxes must be enabled for the sandbox. You can do this when you create or refresh the sandbox. Check your Salesforce release notes and setup options, because the sandbox settings can change between releases.
A scratch org is the simplest case. This command creates one, and tracking is active from the start:
sf org create scratch --definition-file config/project-scratch-def.json --alias my-scratch --set-defaultWhat tracking gives you
In a tracked org, the CLI compares your local files with the org and moves only what differs. You do not list components yourself. These two commands deploy only local changes and retrieve only org changes:
sf project deploy start --target-org my-scratch
sf project retrieve start --target-org my-scratchWithout tracking, the CLI cannot compute that difference. If you run the same commands against an untracked org with no source flags, you get an error or an empty result, depending on the command. You must say what to deploy or retrieve.
Selecting source in an untracked org
You have three ways to name the source. Each one is a flag on sf project deploy start:
sf project deploy start --target-org my-sandbox --source-dir force-app/main/default/classes
sf project deploy start --target-org my-sandbox --metadata ApexClass:MyClass
sf project deploy start --target-org my-sandbox --manifest manifest/package.xml--source-dirdeploys every component in a directory.--metadatadeploys components by type and name.--manifestdeploys what apackage.xmlfile lists.
The retrieve command accepts the same three flags. Later chapters cover this workflow in more detail.
Checking which kind of org you have
The CLI does not ask you to declare the org type. It reads the org's capabilities when you authorize it and when you run a command. A practical check is to run a preview, which lists pending changes without moving anything:
sf project deploy preview --target-org my-scratchIf the org is tracked, the output lists local changes. If it is not, the command reports that it cannot determine changes and asks you to specify the source. Treat that message as a sign that the org is untracked, not as a broken project.
Common mistakes
- Expecting a full sandbox to track changes. Tracking is not available there.
- Assuming a sandbox is tracked because you created it recently. Confirm tracking was enabled for it.
- Running deploy with no flags against production and expecting only your edits to move.
- Mixing up the two sides. Tracking records changes on the org and changes in your local project, so a tracked org still reports conflicts when both sides changed.
Exercise
You run sf project deploy start --target-org my-fullcopy with no source flags, and the CLI cannot work out what to deploy. Why does this happen, and what are three ways to fix it?
Show answer
A Full sandbox does not support source tracking, so the CLI cannot compute which components changed. You must name the source, using --source-dir for a directory, --metadata for components by type and name, or --manifest for a package.xml file.