Exploring Data Export/Import using SFDX

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.

  1. Establish credentials and access to a Trailhead org.
  2. Authenticate into the org using sfdx auth:web:login -d -a DataMig using whatever alias you would prefer.
  3. 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 (Contact, Opportunity) have a look up to the parent object in the outer query (Account).

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:

  1. 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-OUT directory 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.

  1. 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.

Constructing a Query for a Multiple Object Data Model