Import / Export Data via SFDX
How to import / export data from Org to Org via the
sfdxCLI
Exporting Data from a Scratch Org
Export data created in a Scratch Org via a SOQL Query:
sfdx force:data:tree:export --query \
"SELECT Id, Name, Title__c, Phone__c, Mobile_Phone__c, \
Email__c, Picture__c, \
(SELECT Name, Address__c, City__c, State__c, Zip__c, \
Price__c, Title__c, Beds__c, Baths__c, Picture__c, \
Thumbnail__c, Description__c \
FROM Properties__r) \
FROM Broker__c" \
--prefix export-demo --outputdir sfdx-out --plan
This produces a couple of JSON files, one for each Object pulled, plus one for the overall plan. This plan looks something like this:
[
{
"sobject": "Broker__c",
"saveRefs": true,
"files": ["brokers-data.json"]
},
{
"sobject": "Property__c",
"resolveRefs": true,
"files": ["properties-data.json"]
}
]
An example Object-specific file looks like this:
{
"records": [
{
"attributes": {
"type": "Property__c",
"referenceId": "18HenryStRef"
},
"Name": "Stunning Victorian",
"Address__c": "18 Henry St",
"City__c": "Cambridge",
"State__c": "MA",
"Zip__c": "01742",
"Price__c": 975000,
"Title__c": "Stunning Victorian",
"Beds__c": 4,
"Baths__c": 3,
"Location__Longitude__s": -71.11095,
"Location__Latitude__s": 42.35663,
"Picture__c": "https://s3-us-west-1.amazonaws.com/sfdc-demo/realty/house01.jpg",
"Thumbnail__c": "https://s3-us-west-1.amazonaws.com/sfdc-demo/realty/house01sq.jpg",
"Description__c": "Lorem ipsum dolor sit amet",
"Broker__c": "@CarolineBrookerRef"
}
]
}
Important Notes
- In order to maintain relationships between records despite IDs being different from org to org, SFDX utilizes things called
referenceIds,
Import Data into Another Org
Import data exported by the export command like so:
sfdx force:data:tree:import --targetusername test-wvkpnfm5z113@example.com \
--plan sfdx-out/export-demo-Broker__c-Property__c-plan.json
This follows the plan created via the export command and creates all records as they should exist.
Key Learnings / Observations:
- As part of an unlocked package or
sfdxproject, you can include data that will be imported for testing or for deployments- Could be used for CPQ implementations or other implementations that require records that aren’t stored in Custom Metadata.