Sub topic 4
Exporting a Dataset from a Source Org
This chapter assumes you have the Salesforce CLI (sf) installed and authorized to the source org, and a working knowledge of SOQL, including parent-to-child subqueries. You should also know the Account and Contact relationship and the layout of a Salesforce DX project.
By the end you will be able to export related records into JSON files and a plan file. You will also know what the export keeps, what it drops, and which fields cause trouble on import.
What the export command does
sf data export tree runs a SOQL query against an org and writes the results to JSON files in a format that sf data import tree can read back. Two properties make it useful for seeding:
- It replaces real record IDs with reference IDs, such as
AccountRef1. Real IDs are specific to one org, so they can't be loaded into another. - It keeps the relationships between records. A child record points at its parent by reference ID instead of by real ID.
The command is meant for small, curated datasets. A single query returns at most 2,000 records. For larger volumes, use the bulk commands covered in a later chapter.
Running an export
The query below selects ten Technology Accounts and their Contacts in one statement. The (SELECT ... FROM Contacts) part is a parent-to-child subquery, and it is how you tell the command which child records to bring along.
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, or the path to a file that contains one. |
| `--plan` | Writes a plan file and splits the records into one data file per object. |
| `--output-dir` | The directory for the generated files. It is created if it does not exist. |
| `--prefix` | Adds a prefix to the generated file names. |
| `--target-org` | The alias or username of the org to read from. Despite the name, this is the source org. |
Use a query file for anything longer than one line. It is easier to review and to keep in version control:
sf data export tree \
--query data/queries/account-contact.soql \
--plan \
--output-dir data \
--target-org my-source-orgExport with and without `--plan`
The --plan flag changes the shape of the output. Without it, you get a single JSON file in which child records are nested inside their parent. With it, you get one data file per object plus a plan file that lists the files in the order they must be imported.
| Row | Without `--plan` | With `--plan` |
|---|---|---|
| Data files | One file, children nested in parents | One file per object |
| Plan file | None | Yes |
| Relationships | Expressed by nesting | Expressed by reference IDs |
| Best for | Two levels, one query | Several objects, or reuse across steps |
For anything you plan to commit and reuse, use --plan. The plan file is explicit about load order, and the data files are easier to read in diffs. The exact file names depend on the objects in your query, so list the output directory after the first run and commit what you find.
What the generated files contain
Open the Contact data file after an export. Each record carries an attributes block with its object type and a generated reference ID. The parent link is an @-prefixed reference to the parent's reference ID:
{
"records": [
{
"attributes": { "type": "Contact", "referenceId": "ContactRef1" },
"FirstName": "Ada",
"LastName": "Lovelace",
"Email": "[email protected]",
"AccountId": "@AccountRef1"
}
]
}@AccountRef1 says "use the new ID of the record I loaded earlier under the reference ID AccountRef1". The import step resolves it once the Account exists in the target org.
The plan file tells the importer which file to load first and how to handle references:
[
{
"sobject": "Account",
"saveRefs": true,
"resolveRefs": false,
"files": ["Accounts.json"]
},
{
"sobject": "Contact",
"saveRefs": false,
"resolveRefs": true,
"files": ["Contacts.json"]
}
]saveRefs: true on the Account entry means the importer records the new Account IDs. resolveRefs: true on the Contact entry means it replaces each @AccountRef1 with the matching new ID. The next chapter covers these settings in detail. For now, note that the order of entries matters, because parents must come first.
Choosing fields deliberately
The export writes exactly the fields you select, and the import tries to write every one of them. A field that can be read but not written will make the import fail, so treat the field list as a design decision.
Include:
- The fields your tests or demos need.
- Every field that is required on the object, such as
LastNameon Contact. - The parent lookup that links the object to the rest of the dataset. In a subquery, the platform includes the link for you.
Leave out:
- Formula, roll-up summary, and auto-number fields. They are calculated by the platform and can't be written.
- System fields such as
CreatedDate,LastModifiedById, andSystemModstamp. OwnerId. User IDs differ between orgs, and a user from the source org will not exist in a scratch org.RecordTypeId, unless you know the target org has record types with the same IDs. Record type IDs differ between orgs, so it is safer to set record types in a later step by developer name.- Lookups to objects that are not in the export. Their IDs will not resolve in the target org.
Keeping the dataset small and relevant
A seed dataset should be big enough to exercise your features and small enough to load in seconds. Some practical habits:
- Filter with a
WHEREclause that selects a coherent slice, such as one industry or one region, instead of relying onLIMITalone. - Add an
ORDER BYso the same query returns the same rows next time.LIMITwithout an order returns an arbitrary set. - Keep related records together. If you export Contacts without their Accounts, the lookups will point at records that are not in the dataset.
- Count the records before you export. The 2,000-record cap applies to the query, so a parent query that returns 10 Accounts and a subquery that returns thousands of Contacts can still hit it.
Reviewing the output before you commit
Run these checks after each export:
- List the output directory and confirm the plan file and data files exist.
- Open each data file and look for real IDs, email addresses, or phone numbers you did not intend to share.
- Confirm that every
@reference points at a reference ID that exists in an earlier file. - Commit the files to your DX project, usually under
data/, so the whole team seeds from the same dataset.
Common errors at export time
- The query fails with an INVALID_FIELD error. A field name is misspelled, or the field is not visible to the user you authorized. Run the same query in the org's query editor to find the cause.
- The command reports that the query returned too many records. Tighten the
WHEREclause or lower theLIMIT. For larger sets, switch to bulk commands. - The files export fine but fail on import. The cause is usually a field that can't be written, or a lookup to a record outside the dataset. Fix it in the query and export again, instead of hand-editing the JSON.
Exercise
You export Accounts and their Contacts with sf data export tree --plan. The import into a scratch org later fails because it can't write some of the Contact fields you selected. Name two kinds of fields you should have left out of the query. Also say why OwnerId is a poor choice for seed data.
Show answer
Leave out fields the platform calculates or controls, such as formula fields, roll-up summary fields, auto-number fields, and system fields like CreatedDate. These can be read but not written. Leave out OwnerId because user IDs belong to the source org, so the user it points at won't exist in the scratch org.