Newer Version Available
Example: Export and Import Data Between Orgs
While this example is a bit contrived, it’s useful for getting some hands-on experience with the data commands. The example refers to the Broker and Properties custom objects of the Salesforce DreamHouse LWC application example on GitHub. Let’s say that the scratch org from which you want to export data has the alias org-with-data. Similarly, the scratch org into which you want to import data has the alias org-needs-data. For the org-with-data scratch org, it’s assumed that you have already:
- Created the Broker and Properties custom objects by deploying the DreamHouse source.
- Assigned the permission set.
- Populated the objects with the data.
For the org-needs-data scratch org, however, it’s assumed that you’ve created the Broker and Properties objects and assigned the permission set but not yet populated the objects with data.
See the README in the dreamhouse-lwc GitHub repo for instructions on these prerequisite tasks.
-
Export the data from the org-with-data scratch
org.
Use the data query command to fine-tune the SELECT query so that it returns the exact set of data you want to export. This command outputs the results to your terminal or command window, but it doesn’t generate any files.
1sf data query --target-org org-with-data --query \ 2 "SELECT Id, Name, Title__c, Phone__c, Mobile_Phone__c, \ 3 Email__c, Picture__c, \ 4 (SELECT Name, Address__c, City__c, State__c, Zip__c, \ 5 Price__c, Beds__c, Baths__c, Picture__c, \ 6 Thumbnail__c, Description__c \ 7 FROM Properties__r) \ 8 FROM Broker__c" -
When you’re satisfied with the SELECT statement, use it to export the data into a set
of JSON files with the data export tree command.
1sf data export tree --target-org org-with-data --query \ 2 "SELECT Id, Name, Title__c, Phone__c, Mobile_Phone__c, \ 3 Email__c, Picture__c, \ 4 (SELECT Name, Address__c, City__c, State__c, Zip__c, \ 5 Price__c, Beds__c, Baths__c, Picture__c, \ 6 Thumbnail__c, Description__c \ 7 FROM Properties__r) \ 8 FROM Broker__c" \ 9 --prefix export-demo --output-dir sfdx-out --planThe data export tree command writes the JSON files to the sfdx-out directory and prefixes each file name with the string export-demo. The files include a plan definition file, which refers to the other files that contain the data, one for each exported object (Broker and Properties).
-
Import the data into the org-needs-data scratch
org by specifying the plan definition file to the data import
tree command.
1sf data import tree --target-org org-needs-data \ 2 --plan sfdx-out/export-demo-Broker__c-Property__c-plan.jsonUse the --plan parameter to specify the full path name of the plan execution file generated by the data export tree command. Plan execution file names always end in -plan.json.
Example
Looking for a more complicated example? The easy-spaces-lwc sample app has a data plan showing how to import Accounts, related Contacts, and a 3-level deep custom object chain.