Exploring Data Export/Import using SFDX
These notes are a worked example based on a Salesforce help article, called "Example: Export and Import Data Between Orgs."
This example considers a situation where you have a set of perfect test data in a scratch org and need to move that test data into a second, empty scratch org. Here I export the default data from a Trailhead org and reimport it.
- Establish credentials and access to a Trailhead org.
- Authenticate into the org using
sfdx auth:web:login -d -a DataMigusing whatever alias you would prefer.
- Work on a query using
sfdx data query --target-org DataMig --query ""and then add the query between the double quotes. This command outputs the query results in the terminal. For example the following pulls the data from the indicated fields from all Accounts, Contacts, and Opportunities. Make sure to include all “required” fields or else the import may error out on one of the objects, but can succeed on the rest.
SELECT Id, Name, (SELECT Id, FirstName, LastName FROM Contacts), (SELECT Id, Name, StageName, CloseDate, Amount FROM Opportunities) FROM Account
returns the following:
Note this type of query is possible because the child objects queried in the inner query (
Opportunity) have a look up to the parent object in the outer query (
In developing the query in step 3, note you can also write queries to return a list of all fields on a given object. For example:
SELECT Name FROM EntityParticle WHERE EntityDefinition.QualifiedApiName ='Account'
returns the following:
- Once you’re satisfied with the output of the query, run
sfdx data export tree --target-org DataMig --query "" --prefix export-demo --output-dir sfdx-out --plan, adding the query between the double quotes. This command exports the data into a set of JSON files in the
SFDX-OUTdirectory and prefixes each file name with
export-demo. It also creates a plan definition file, which refers to the other files that contain the data.
- Import the data into the target org by specifying the plan definition file using the following command:
sfdx data import tree --target-org DataMig --plan sfdx-out/export-demo-Account-Contact-Opportunity-plan.json. Note that the import command can partially succeed. The results that will print on successful execution are shown below.
Data Storage Considerations for Personal Projects
- Trailhead orgs come with 160 records, totalling ~380 KB, or 7% of the 5.0 MB limit on Trailhead orgs. The records consume either 2 or 8 KB each, averaging 2.4 KB per record. So, Trailhead orgs should be able to hold over 2000 additional records before the data storage is consumed.
- Scratch orgs have a 200 MB limit, but are limited to a 30 day lifespan. ~80,000 records should be sufficient for many personal projects, but it may be necessary to migrate the data to new scratch orgs from time to time.