Resources /
Blog

How to Enable Source Tracking in Salesforce Sandboxes and Scratch Orgs

Submit your details to get a book

8
Min Read
Resources /
Blog

How to Enable Source Tracking in Salesforce Sandboxes and Scratch Orgs

Download

Submit your details to get a book

8
Min Read
Glowing sandbox cube on a hex platform beside code panels, linked to sync, clock, list and check tiles
Glowing sandbox cube on a hex platform beside code panels, linked to sync, clock, list and check tiles

To enable source tracking in Salesforce, you switch it on from Dev Hub in production for all Developer and Developer Pro sandboxes, or in Sandbox Settings for just one. Scratch orgs have it already, while the DX Developer Guide says Partial Copy sandboxes, Full sandboxes and Developer Edition orgs can't use it.

Once it's on, Salesforce CLI can preview, retrieve and deploy only the metadata that changed. Below, you'll find both ways to enable it, the permissions you need, the CLI commands and the cases where you'd switch it off.

What is source tracking in Salesforce?

Source tracking is how DX tools keep an eye on an org for you, flagging metadata components you've added, changed or deleted. Since source tracking identifies what changed, you don't have to track changes manually anymore, and you just pick which ones go into a release.

It works by comparing the source files in your local project with the metadata in your org, then keeping the two in sync. On top of a plain deploy, the guide says it also lets you:

Source tracking has no direct effect on the org itself and affects only your local environment, where Salesforce CLI checks a configuration file. The Metadata Coverage report shows which metadata components support it.

Which orgs support source tracking?

Support depends on the org type, and our guide to Salesforce sandbox types covers how each sandbox differs. The table below sums up what the DX guide says.

Org typeSource trackingHow it's turned on
Scratch orgAlways supportedSource tracking enabled by default; opt out with the --no-track-source flag
Developer and Developer Pro sandboxSupportedDev Hub in production, or Sandbox Settings in the sandbox
Partial Copy and Full sandboxNot supportedCan't be enabled
Production orgNot supportedDeploy and retrieve with --source-dir or --metadata instead
Developer Edition orgNot supportedCan't be enabled

Source tracking works only if your target org allows it. In an org without source tracking, you can still deploy or retrieve using an appropriate flag, but the commands don't check for conflicts.

How do you enable source tracking in Salesforce?

There are two ways to turn on source tracking for your org, and the right one depends on how many sandboxes you use. Both need the user permissions listed at the end of this section.

For all Developer and Developer Pro sandboxes

Turn it on once in production if your team works in many sandboxes. Per the DX guide, every newly created or refreshed Developer and Developer Pro sandbox then uses it, and these are the steps:

  1. Log in to the source org, which is your production org
  2. From Setup, find and select Dev Hub
  3. Select Enable Source Tracking in Developer and Developer Pro Sandboxes
  4. Refresh any existing Developer or Developer Pro sandboxes, because existing ones don't get it until they're refreshed

The Dev Hub option is available in Enterprise, Performance and Unlimited Editions. In Professional and Database.com Editions, you can only enable source tracking directly in the sandbox, which is the next method.

In one sandbox

Use this method when you don't want tracking in every sandbox, or when you want it now without a refresh. According to the DX guide, you enable it from the sandbox's own Settings Setup page:

  1. Log in to the Developer or Developer Pro sandbox
  2. From Setup, find and select Sandbox Settings
  3. Click Enable Source Tracking in This Sandbox

Tracking starts from that moment, and changes made before you enabled source tracking aren't tracked. If the sandbox already holds work you need, retrieve that metadata into your project first so your repository has a starting point.

Refreshing a sandbox wipes all of its source tracking information. Unless production has the Dev Hub setting on, you must re-enable it in Sandbox Settings after each refresh.

Think twice before you disable source tracking, too. The guide warns that it can take several days to clean up its records, and you can only re-enable it after that cleanup finishes.

Permissions required

If you can't see the sandbox options in Setup, check your permissions first. The DX guide lists these for both methods.

How do you use source tracking with the CLI?

Start by connecting the CLI to the sandbox. The DX guide recommends your My Domain login URL, passed with the --instance-url flag of org login web:

Terminal: sign in to the sandbox
sf org login web --alias DevSandbox --instance-url https://MyDomainName--SandboxName.sandbox.my.salesforce.com

From there, the daily loop is preview, retrieve, commit and deploy, inside the project structure our Salesforce DX guide explains:

Each preview lists the metadata type, API name and local path, and a blank path usually means the component exists only in the org. To work on one piece of code, specify it with a flag, as the guide does with --metadata ApexClass:MyFabClass for an Apex class.

You can also switch tracking for an existing org with sf org enable tracking or sf org disable tracking. The enable command returns an error for an org that doesn't support it, such as a Full sandbox.

Profiles behave differently: with tracking on, a profile retrieve returns permissions for your manifest plus every tracked component. The guide recommends permission sets instead, and our posts on permission sets and profile deployment go deeper.

Resolving conflicts

If someone changed the same component in the org and in your project, the preview flags a conflict and the CLI stops the deploy or retrieve. Resolve it by hand, or decide which version wins and overwrite the other with the --ignore-conflicts flag.

The guide's advice is to retrieve changes and resolve conflicts before you deploy to a sandbox. If tracking ever drifts out of sync, the same flag gets it back: deploy start if you trust your local files, or retrieve start if you trust the org.

When to turn source tracking off

Tracking isn't free: the CLI runs extra queries and waits for the SourceMember Tooling API object to update, so some commands take longer on medium or large projects. It can slow deployments and retrievals, which is why the guide says you're sometimes better off disabling it when you don't need it.

The guide counts a project as medium at 30 or more components or 50 or more tests, and as large at 600 or more components or 150 or more tests. If long-running commands drag on, it suggests you work with smaller sets of components.

The example the guide gives is a continuous integration script that creates a scratch org, deploys source and runs tests. You'd just add the --no-track-source flag to opt out of tracking when you create a scratch org or sandbox, and it only changes your local configuration.

There's one place you shouldn't do this. If a scratch org or sandbox is a development environment in Lifecycle Center (formerly DevOps Center), the guide says to leave tracking on.

How source tracking fits into a release

You'll repeat this loop a lot: in the DX guide's example, you create a custom object in Setup or an Apex class using an IDE such as VS Code. From there, it's preview, retrieve, deploy and repeat until the app is done, just like the infographic shows.

Infographic of source tracking in Salesforce in six steps: check the org type, enable it for all sandboxes from Dev Hub or in one sandbox, sign in with the CLI, preview changes, retrieve and commit to Git, then deploy and resolve conflicts

Just remember where tracking stops. Once your changes move on to Partial Copy, Full or production orgs, source tracking isn't supported, and even where it works it only follows metadata, not record data.

If you need to know who changed what in production, you'll want the platform's own logs instead. Our audit trail guide walks through setup changes, and our Field Audit Trail guide covers changes to field values.

Track changes across your orgs with Flosum

Flosum DevOps runs in Flosum's cloud or in your own cloud. Its Organizations page shows you every org you can use for metadata retrieval, change viewing and deployment, and you can filter the list by org type.

A Flosum Retrieve job pulls down only the components that changed since the last retrieval, and it runs automatically every hour. You can still kick one off by hand before you commit or deploy a branch.

Each org's Components tab tells you which user account last changed each component in Salesforce, and when. With the Git integration, Flosum syncs its branches to your repository, and each sync job creates a new commit for traceability.

Before a release, Validate checks a branch against the target org without deploying. Overwrite Protection runs against the org to prevent accidental overwrites of metadata, as our post on Salesforce overwrite protection explains.

FAQs

How do I enable source tracking in Salesforce?

In production, select Dev Hub in Setup and turn on Enable Source Tracking in Developer and Developer Pro Sandboxes, then refresh existing sandboxes. For one sandbox, open Sandbox Settings in that sandbox and click Enable Source Tracking in This Sandbox.

Does source tracking work in production, Partial Copy or Full sandboxes?

No. Per the DX guide, source tracking isn't supported in production orgs, Partial Copy sandboxes, Full sandboxes or Developer Edition orgs, only in scratch orgs and Developer or Developer Pro sandboxes.

What happens to source tracking when you refresh a sandbox?

A refresh deletes all source tracking information. If production has the Dev Hub setting on, the refreshed sandbox gets tracking again; if not, re-enable it in Sandbox Settings.

Does source tracking slow down deployments?

It can. The DX guide says tracking runs extra queries, so some commands take longer on medium and large projects, and CI scripts can opt out with --no-track-source.

Book a demo to see how Flosum tracks metadata changes across all your orgs.

Table Of Contents
Author
Stay Up-to-Date
Get flosum.com news in your inbox.

Thank you for subscribing

Ship Salesforce changes faster
Teams on Flosum report 40% higher development velocity and 75%
fewer merge conflicts.
Free to start  |  No credit card required