Sub topic 4
Exporting a dataset from a source org
This chapter assumes you have the Salesforce CLI (sf) installed and authorized to a source org. You should also know the basic data model (objects, lookups, master-detail) and have a Salesforce DX project. The source org can be a sandbox or a Dev Hub-connected org that already holds good sample records.
What the export command does
sf data export tree runs a SOQL query and writes the results as JSON. It follows relationships in the query, so a parent and its child records can be exported together. The JSON is what the next chapters use to rebuild the data in a scratch org.
You can export in two shapes:
- Without
--plan, you get a single JSON file for the query. This is fine for one object. - With
--plan, you get one data file per object and a plan file that says in what order to import them. Use this for related objects.
Export Accounts with their Contacts
The query below selects Technology Accounts and, in a subquery, each Account's Contacts.
sf data export tree \
--query "SELECT Id, Name, Industry, (SELECT Id, FirstName, LastName, Email FROM Contacts) FROM Account WHERE Industry = 'Technology' LIMIT 10" \
--plan \
--output-dir data \
--target-org my-source-orgThe flags do the following:
| Flag | Purpose |
|---|---|
| `--query` | The SOQL query. The subquery in parentheses pulls child records. |
| `--plan` | Writes separate data files and a plan file. |
| `--output-dir` | The folder for the output. Here it is `data`. |
| `--target-org` | The alias or username of the org to read from. |
Keep the LIMIT. A seed dataset should be small enough to read and import quickly.
What you get
With --plan, the data directory contains files like these:
Account-Contact.json, or one file per object, holding the records.Account-Contact-plan.json, the plan file.
Each record gets a referenceId, such as AccountRef1. The CLI uses these IDs to link children to parents. It does this instead of the real 18-character Salesforce IDs, which would mean nothing in another org. The next chapter covers how the plan file uses them.
Limits to know about
The tree export is meant for small datasets. Keep these limits in mind:
- A single query returns up to 2,000 records in total, counting parents and children.
- It handles one level of parent-to-child nesting per query, as in the example above.
- Records must be readable by the user you authorized. Field-level security can silently remove fields from the output.
- Some fields cannot be imported, such as system-generated and formula fields. Remove them from the query to avoid problems later.
For larger volumes, see the chapter on bulk loading.
Check the output before you commit it
Open the generated files and look at them before you add them to version control.
- Confirm the record counts match what you expected.
- Search for real emails, phone numbers, and names. Replace them with fake values if the source is a copy of production.
- Check that each child has a reference to a parent in the file.
Exercise
Why does sf data export tree --plan give each record a referenceId instead of keeping the original Salesforce ID?
Show answer
Record IDs are specific to the source org and do not exist in the target org. Reference IDs let the import step link children to their parents, and the target org assigns new IDs when the records are created.
Exercise
You export Accounts with a Contacts subquery and notice the output has no Email values, although the field is in your query. What is one likely cause?
Show answer
The authorized user may not have field-level security access to Email, so the field is left out of the results. Check the user's permissions in the source org.