Newer Version Available
Develop Against Any Org (Beta)
Imagine if you could use the Salesforce CLI to retrieve and deploy metadata to non-source-tracked orgs with the ease of pushing and pulling source to and from scratch orgs. And best of all, no extra conversion steps are required! After you retrieve the metadata, you don't have to convert it to source format. When you're ready to deploy it back to the org, you don't have to convert it to metadata format. If you’re new to Salesforce CLI, Salesforce DX Project Structure and Source File Format explains the difference between source format and metadata format.
Using force:source:retrieve, you can retrieve the metadata you need in source format to your local file system (DX project). Once you're ready to test, you can use force:source:deploy to deploy your local files directly to a non-source-tracked org.
So, how do these source commands differ from the scratch org commands, source:push and source:pull? Because the changes aren't tracked, you retrieve or deploy all the specified metadata instead of only what's changed. The source you retrieve or deploy overwrites what’s you have locally or in your org, respectively.
Not sure what metadata types are supported or which metadata types support wild cards in package.xml? See Metadata Types in the Metadata API Developer Guide.
Before You Begin
- Create a Salesforce DX project.
- Authorize your non-source-tracked org. If connecting to a sandbox, edit your sfdx-project.json file to set sfdcLoginUrl to https://test.salesforce.com before you authorize the org. Don't forget to create aliases for your non-source-tracked orgs.
Metadata Names That Require Encoding on the Command Line
When retrieving or deploying metadata using the --metadata option, commas in metadata names require encoding to work properly.
Don’t: sfdx force:source:deploy -m "Profile:Standard User,Layout:Page,Console"
Do: sfdx force:source:deploy -m "Profile:Standard User,Layout:Page%2CConsole"
Retrieve Source from a Non-Source-Tracked Org
Use the force:source:retrieve command to retrieve source from orgs that don’t have source tracking, such as sandboxes. If you already have the source code and metadata in a VCS, you might be able to skip this step. If you're starting anew, you retrieve the metadata associated with the feature, project, or customization you're working on.
- Specify a package.xml that lists the components to retrieve.
- Specify a comma-separated list of metadata component names.
- Specify a comma-separated list of source file paths to retrieve. You can use the source path option when source exists locally, for example, after you've done an initial retrieve.
If the comma-separated list you’re supplying contains spaces, enclose the entire comma-separated list in one set of double quotes.
| To Retrieve: | Command Example |
|---|---|
| All metadata components listed in a manifest | sfdx force:source:retrieve -x path/to/package.xml |
| Source files in a directory | sfdx force:source:retrieve -p path/to/source |
| A specific Apex class and the objects whose source is in a directory | sfdx force:source:retrieve -p path/to/apex/classes/MyClass.cls,path/to/source/objects |
| Source files in a comma-separated list that contains spaces | sfdx force:source:retrieve -p "path/to/objects/MyCustomObject/fields/MyField.field-meta.xml, path/to/apex/classes" |
| All Apex classes | sfdx force:source:retrieve -m ApexClass |
| A specific Apex class | sfdx force:source:retrieve -m ApexClass:MyApexClass |
| A layout name that contains a comma (Layout: Page, Console) | sfdx force:source:retrieve -m "Layout:Page%2C Console" |
Deploy Source to a Non-Source-Tracked Org
Use the force:source:deploy command to deploy source to orgs that don’t have source tracking, such as a sandbox.
You can deploy metadata in source format using these methods:
- Specify a package.xml that lists the components to deploy
- Specify a comma-separated list of metadata component names
- Specify a comma-separated list of source file paths to deploy
If the comma-separated list you’re supplying contains spaces, enclose the entire comma-separated list in one set of double quotes.
| To Deploy: | Command Example |
|---|---|
| All components listed in a manifest | sfdx force:source:deploy -x path/to/package.xml |
| Source files in a directory | sfdx force:source:deploy -p path/to/source |
| A specific Apex class and the objects whose source is in a directory | sfdx force:source:deploy -p path/to/apex/classes/MyClass.cls,path/to/source/objects |
| Source files in a comma-separated list that contains spaces | sfdx force:source:deploy -p "path/to/objects/MyCustomObject/fields/MyField.field-meta.xml, path/to/apex/classes" |
| All Apex classes | sfdx force:source:deploy -m ApexClass |
| A specific Apex class | sfdx force:source:deploy -m ApexClass:MyApexClass |
| All custom objects and Apex classes | sfdx force:source:deploy -m CustomObject,ApexClass |
| All Apex classes and a profile that has a space in its name | sfdx force:source:deploy -m "ApexClass, Profile:Content Experience Profile" |
Delete Non-Tracked Source
Use the force:source:delete command to delete components from orgs that don’t have source tracking, such as a sandbox.
You can delete metadata in source format by specifying the path to the source or by listing individual metadata components, once the source exists locally in a Salesforce DX project. If the comma-separated list you’re supplying contains spaces, enclose the entire comma-separated list in one set of double quotes.
| To Delete: | Command Example |
|---|---|
| Source files in a directory | sfdx force:source:delete -p path/to/source |
| A specific component, such as a FlexiPage | sfdx force:source:delete -m FlexiPage:Broker_Record_Page |
Do You Want to Retain the Generated Metadata?
Normally, when you run some CLI commands, a temporary directory with all the metadata is created then deleted upon successful completion of the command. However, retaining these files can be useful for several reasons. You can debug problems that occur during command execution. You can use the generated package.xml when running subsequent commands, or as a starting point for creating a manifest that includes all the metadata you care about.
To retain all the metadata in a specified directory path when you run these commands, set the SFDX_MDAPI_TEMP_DIR environment variable:
- force:source:deploy
- force:source:retrieve
- force:source:delete
- force:source:push
- force:source:pull
- force:source:convert
- force:org:create (if your scratch org definition contains scratch org settings, not org preferences)
Example: