Newer Version Available
Example: Export and Import Data Between Orgs
This use case refers to the Broker and Properties custom objects of the Salesforce DX Github DreamHouse example. It’s assumed that, in the first scratch org from which you are exporting data, you’ve created the two objects by pushing the DreamHouse source. It’s also assumed that you’ve assigned the permission set and populated the objects with the data. In the second scratch org, however, it’s assumed that you’ve created the two objects and assigned the permission set but not yet populated them with data. See the README of the dreamhouse-sfdx GitHub example for instructions on these tasks.
-
Export the data in your default scratch org.
Use the force:data:soql: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 change the data in the org. Because the SOQL query is long, the command is broken up with backslashes for easier reading. You can still cut and paste the command into your terminal window and run it.
1sfdx force:data:soql:query --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, Title__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.
1sfdx force:data:tree:export --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, Title__c, Beds__c, Baths__c, Picture__c, \ 6 Thumbnail__c, Description__c \ 7 FROM Properties__r) \ 8 FROM Broker__c" \ 9 --prefix export-demo --outputdir sfdx-out --planThe export command writes the JSON files to the sfdx-out directory (in the current 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.
-
Import the data into the new scratch org by specifying the plan definition file.
1sfdx force:data:tree:import --targetusername test-wvkpnfm5z113@example.com \ 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 force:data:tree:export command. Plan execution file names always end in -plan.json.
In the previous example, you must use the --targetusername option because you are importing into a scratch org that is not your default. Use the force:org:list command to view all your scratch orgs along with their usernames and aliases. You can also use config:set to set the new scratch org as your default.
-
(Optional) Open the new scratch org and query the imported data using the Salesforce UI and
SOQL.
1sfdx force:org:open --targetusername test-wvkpnfm5z113@example.comIf you set an alias for the scratch org username, you can pass it to the --targetusername parameter.
1sfdx force:org:open --targetusername <alias>
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.