Friday 8 November 2019

Import and Export Data using SalesforceDX (SFDX)

Import and Export Data using SalesforceDX (SFDX)

In this post we will talk about Data migration using SalesforceDX. We have a vary simple use case to Import and Export data from one Salesforce Org to another Salesforce Org. We know we can do the same with dataloader or some other custom tool. But in this post we will talk about how to import and export with SalesforceDX (SFDX). If you are new to SalesforceDx please check this ApexHours Session Recording by Jitender Zaa.

How to export records with SFDX

 Step 1) First Login to your Salesforce Org from where you want to export data

sfdx force:auth:web:login --setalias myDev1

Step 2) Use below command to export the data

To Export the data we need to use "sfdx force:data:tree:export" command and we need to pass one Soql query. You can also pass the path of SOQL query file with the help of -q. Lets export the Account and its all related contacts records.

sfdx force:data:tree:export -q "SELECT Id,Name,(Select FirstName,LastName from Contacts) FROM Account limit 2" -d ./data -p -u myDev1

-q : SOQL query or file path of soql query.
-d : directory, where exported json file can be saved
-u : Salesforce user to perform operation
-p : generate multiple sobject tree files and a plan definition file for aggregated import

Once you execute above command it will create three file for you in Data Folder.
  • Accounts.json
  • Contacts.json
  • Account-Contact-plan.json

How to Import data using Salesforce DX

First login to salesforce org in which you want to import the data. Then use below command to Import records in Salesforce instance, which we just exported in previous step

sfdx force:data:tree:import -p data/Account-Contact-plan.json -u myDev1

-f : JSON file to be imported
-p : plan file
-u : Salesforce user to perform operation

The query for export can return a maximum of 2,000 records. For more information, see the REST API Developer Guide:

NOTE: For above use case you don't need any scratch org. You can simply login in any sandbox and migrate your data between two org's.

There is another Use case where we can use SalesforceDx for Import and Export Data.

Use Case: If you are implementing Continuous Integration (CI/CD) solution built for Salesforce, Some time you need to upload sample data into Salesforce.

Solution: For that you need to use command line dataloader or custom tool built to import export data automatically. We can use SalesforceDX for data import and export as well for this requirement.

Related Post :

Further Reading:

Amit Chaudhary

No comments:

Post a Comment