Newer Version Available
Track Changes Between the Project and the Sandbox (Beta)
This feature is available in Developer and Developer Pro Sandboxes only.
For setup instructions, see the Salesforce CLI Setup Guide.
If you're working in a scratch org instead of a sandbox, see Track Changes Between the Project and Scratch Org.
Authenticate Your Sandbox in Salesforce CLI
Authenticating your Developer or Developer Pro sandbox in the local Salesforce CLI lets them communicate through CLI commands without requiring additional credentials.
We've given the sandbox in this example the alias MyDevSandbox. To authenticate your sandbox in the CLI, run the following command.
1sfdx auth:web:login -r https://test.salesforce.com -a MyDevSandboxYou use the -r flag to override the default login URL (which is the URL for the production org) with the login URL for the sandbox. The sandbox in the example uses the alias MyDevSandbox.
The sandbox login page opens in a web browser. Log into the sandbox using your normal sandbox credentials, accept the authentication request, then close the browser.
Create a New Salesforce DX Project
Sandbox source tracking syncs against a local workspace to identify what's been changed in the sandbox. From a working directory, create a new Salesforce DX project that can serve as the standard of comparison. Let's call our new project MyControlProject.
1sfdx force:project:create -n MyControlProjectYou're now set up to have the sandbox org track changes you make there.
Display Sandbox Changes Identified by Source Tracking
Make a few configuration changes in the sandbox org, then go to the project directory (MyControlProject in the previous section) and display a list of the changes you just made. Remember, MyDevSandbox was the alias assigned to the sandbox that was authenticated in Salesforce CLI.
1cd MyControlProject
2sfdx force:source:status -u MyDevSandboxYou see that a list of changes posted to output:
1STATE FULL NAME TYPE PROJECT PATH
2───────────── ───────── ───────── ───────────────────────────────────────────────────
3Local Deleted MyClass . ApexClass /MyClass.cls-meta.xml
4Local Deleted MyClass . ApexClass /MyClass.cls
5Local Add OtherClass ApexClass /OtherClass.cls-meta.xml
6Local Add OtherClass ApexClass /OtherClass.cls
7Local Add Event QuickAction /Event.quickAction-meta.xml
8Remote Deleted MyWidgetClass ApexClass /MyWidgetClass.cls-meta.xml
9Remote Deleted MyWidgetClass ApexClass /MyWidgetClass.cls
10Remote Changed (Conflict) NewClass ApexClass /NewClass.cls-meta.xml
11Remote Changed (Conflict) NewClass ApexClass /NewClass.clsPull Changed Source from the Sandbox to Your Project
When source tracking is enabled in a sandbox, pulling source from the sandbox to your local source directory moves only the files that changed:
1sfdx force:source:pull -u MyDevSandboxPush Changed Source from Your Project to the Sandbox
If you make changes in a local environment or IDE and your sandbox is set up for source tracking, it's easy to synchronize the sandbox with the local environment. Push the local changes to the sandbox, and source tracking identifies which files have changes and automatically pushes only those files.
1sfdx force:source:push -u MyDevSandboxYou can push local changes to a sandbox with source tracking enabled even if the changes are in a metadata source file that you retrieved to the local workspace. For example, let's say that you wanted to retrieve an ApexClass called MyApexClass to make some updates.
1sfdx force:source:retrieve -m ApexClass:MyApexClass -u MyDevSandboxWhen you're ready to sync your local changes with the sandbox, you could use the force:source:deploy command, but you'd have the extra bother of explicitly specifying the files to deploy.
Instead, because your sandbox is set up for source tracking, a simple force:source:pushcommand automatically pushes only the changed files to the sandbox.
1sfdx force:source:push -u MyDevSandboxWorking with Source Tracking in a Sandbox
When source tracking is enabled in a sandbox that multiple developers collaborate in, who sees who’s changes?
Local workspace changes on one developer’s machine are visible to only that developer. Other developers working in the sandbox aren’t notified by source tracking that another developer has pending changes. After a change is pushed to a sandbox, source tracking notifies other developers that they can pull the change.
Let’s walk through an example scenario.
Say that two developers, Sarah and Tracy, are working in the same sandbox and have identical local workspaces. Source tracking tells them that there are no pending changes between the sandbox and their local workspace.
Now say that Sarah creates an Apex class named SarahsClass in her local workspace.
Before Sarah pushes the Apex class to the sandbox, source tracking tells her that an Apex Class exists in her local workspace but not on the sandbox.
1=== Source Status
2STATE FULL NAME TYPE PROJECT PATH
3───────────── ───────── ───────── ───────────────────────────────────────────────────
4Local Add SarahsClass ApexClass /SarahsClass.cls-meta.xml
5Local Add SarahsClass ApexClass /SarahsClass.cls-meta.xmlSource tracking tells Tracy that there are no pending changes between her local workspace and the sandbox.
1=== Source Status
2No results foundAfter Sarah pushes the Apex class SarahsClass to the sandbox, source tracking tells Sarah that there are no changes between her local workspace and the sandbox.
1=== Source Status
2No results foundSource tracking tells Tracy that there is an Apex class in the sandbox that isn’t in her local workspace.
1=== Source Status
2STATE FULL NAME TYPE PROJECT PATH
3───────────── ───────── ───────── ───────────────────────────────────────────────────
4Remote Add SarahsClass ApexClass
5Remote Add SarahsClass ApexClassTo get the Apex class Sarah created in her local workspace, Tracy now knows to run sfdx force:source:pull.
Retrieving and Pulling Changes to Profiles with Source Tracking
Retrieving and pulling profiles behaves a little differently with source tracking.
Without source tracking, retrieving profiles doesn’t return all profile information. Retrieving profiles returns information about profiles that pertains to other items specified in the packing.xml file.
For example, retrieving profiles with this package.xml file returns profile permissions for the MyCustomField__c custom field on the Account object.
1<?xml version="1.0" encoding="UTF-8"?>
2<Package xmlns="http://soap.sforce.com/2006/04/metadata">
3 <types>
4 <members>Account.MyCustomField__c</members>
5 <name>CustomField</name>
6 </types>
7 <types>
8 <members>*</members>
9 <name>Profile</name>
10 </types>
11 <version>50.0</version>
12</Package>With source tracking, retrieving profiles returns profile information pertaining to anything else specified in the package.xml file plus any entities getting tracked by source tracking. That includes any entity for which a change exists between your local workspace and the org.
For example, say you create a custom field on the Opportunity object called OppCustomField__c in your local environment. Source tracking detects the change and reports it. Now you retrieve profiles using the same package.xml file as you did when source tracking was off.
1<?xml version="1.0" encoding="UTF-8"?>
2<Package xmlns="http://soap.sforce.com/2006/04/metadata">
3 <types>
4 <members>Account.MyCustomField__c</members>
5 <name>CustomField</name>
6 </types>
7 <types>
8 <members>*</members>
9 <name>Profile</name>
10 </types>
11 <version>50.0</version>
12</Package>Even though the package.xml file doesn’t reference OppCustomField__c, because source tracking is tracking the new custom field, your retrieve returns profile permissions for both the MyCustomField__c custom field on the Account object and the OppCustomField__c on the Opportunity object.
For more information about retrieving profiles, see the Profile metadata type in the Metadata API Developer Guide.
Tip: Let Source Tracking Help You Identify Change Set Components
If you're using change sets to migrate customizations, you can use the sfdx force:source:status command to help you identify which components need to be added to the change set. That way, when you're ready to build an outbound change set, you can use the sfdx force:source:status -u MyDevSandbox command to display a list of changed components.