Sub topic 6
Importing the dataset into a scratch org
This chapter covers importing a tree-format dataset into a scratch org with sf data import tree. It assumes you have a Salesforce DX project, an authorized Dev Hub, and a dataset exported as in the previous chapters: JSON data files plus a plan file. You should also know how lookup relationships work and how to read basic JSON.
What the import does
sf data import tree reads JSON records and inserts them into a target org through the REST API. It does not match existing rows. Each run creates new records. The command is not an upsert, so running it twice gives you duplicates. Later chapters cover external IDs and upsert for repeatable loads.
The command accepts input in two forms:
--files: one or more JSON files, each holding records of a single object type.--plan: a plan file that lists data files in a fixed order and says how to handle references between them.
Use --files for a single object or for records with no relationships to other imported records. Use --plan whenever one object points at another, such as Contacts that point at Accounts.
Prepare the scratch org first
Records can only be inserted if the objects, fields, and picklist values exist in the target org. A new scratch org contains only the standard schema plus whatever its definition file enables. Your custom objects, custom fields, record types, and validation rules arrive when you deploy your project.
The order is therefore fixed: create the org, deploy metadata, then import data.
sf org create scratch \
--definition-file config/project-scratch-def.json \
--alias my-scratch \
--set-default \
--duration-days 7
sf project deploy start --target-org my-scratch
sf data import tree --plan data/Account-Contact-plan.json --target-org my-scratchIf you import before deploying, the import fails on any field that does not exist yet. Standard-only datasets can still fail if the scratch definition does not enable a feature the data depends on, such as a particular edition feature or a multi-currency setting.
The plan file
The plan file is a JSON array. Each entry describes one object and the files that hold its records. The order of entries is the order of loading.
[
{
"sobject": "Account",
"saveRefs": true,
"resolveRefs": false,
"files": ["Accounts.json"]
},
{
"sobject": "Contact",
"saveRefs": false,
"resolveRefs": true,
"files": ["Contacts.json"]
}
]Two flags control relationships:
| Flag | Meaning | Typical use |
|---|---|---|
| `saveRefs` | After inserting, remember the new Salesforce ID for each record's `referenceId`. | Parent objects, such as Account. |
| `resolveRefs` | Before inserting, replace `@referenceId` values with the saved IDs. | Child objects, such as Contact. |
A reference can only be resolved if its parent was inserted earlier in the same run. The parent entry must therefore come first, and it must have saveRefs set to true.
How references are written in data files
Each record has an attributes block with its type and a referenceId. A child record points at a parent by putting @ before the parent's referenceId.
{
"records": [
{
"attributes": { "type": "Contact", "referenceId": "ContactRef1" },
"FirstName": "Ada",
"LastName": "Lovelace",
"Email": "[email protected]",
"AccountId": "@AccountRef1"
}
]
}Here AccountId is set to whatever ID the Account with referenceId AccountRef1 received at insert. The matching Account record in Accounts.json looks like this:
{
"records": [
{
"attributes": { "type": "Account", "referenceId": "AccountRef1" },
"Name": "Acme Technology",
"Industry": "Technology"
}
]
}The referenceId is a local label. It only has to be unique within the import run, and it never becomes a field value in the org.
What happens during an import
The CLI processes the plan in order. For each entry it reads the files, swaps any references it can resolve, sends the records, and stores the new IDs if saveRefs is set.
The import is not atomic across the whole plan. If the Contact step fails, the Accounts stay in the org. Clean up before you retry, or you will create duplicate parents.
Reading the output
On success, the command prints a table of the created records, with the reference ID and the new Salesforce ID. On failure, it prints the error for the failing record along with its reference ID. Use the reference ID to find the record in your data file.
To check what landed, query the org:
sf data query \
--query "SELECT Id, Name, (SELECT LastName FROM Contacts) FROM Account WHERE Industry = 'Technology'" \
--target-org my-scratchLimits to know
The tree commands are meant for small datasets. A single import tree call handles a limited number of records per request, and the tooling documents a ceiling on records in a single tree import. Check the current Salesforce CLI reference for the exact number, because it can change. As a rule, keep seed sets in the hundreds of records, not thousands. For larger volumes, use the bulk commands covered in a later chapter.
Other points that affect results:
- Nested records. The export command can write child records nested inside the parent when you use a relationship subquery, but the plan format keeps them in separate files. Importing a plan is the more predictable path.
- Read-only fields. Fields such as
Id,CreatedDate, and formula fields cannot be written. Exports drop most of them, but hand-edited files sometimes keep them and cause errors. - Required fields. Any field marked required in the org, or by a validation rule, must be present in every record.
- Triggers and flows. Automation runs on inserted records just as it does in production. A trigger that sends email or calls out can slow or break a seed load.
- Owner and user references. A record that points at a specific user in the source org will not resolve, because that user does not exist in the scratch org. Remove such fields or map them to a user you create.
Common failures and first checks
| Symptom | Likely cause | First check |
|---|---|---|
| `No such column` or invalid field | Metadata not deployed | Run `sf project deploy start` and retry. |
| Required field missing | Field is required or a validation rule fires | Add the value to every record. |
| Reference not resolved | Parent entry missing, out of order, or `saveRefs` is `false` | Check plan order and flags. |
| Invalid picklist value | Value does not exist in the scratch org | Deploy the value set or edit the data. |
| Duplicate rows after a retry | Earlier partial import left records behind | Delete the earlier records and rerun. |
A later chapter on troubleshooting goes through these in more detail.
Putting it together
A repeatable local flow looks like this:
- Create the scratch org from the definition file.
- Deploy the project metadata.
- Import the plan.
- Query the org to confirm counts.
Because the import is not idempotent, the simplest safe pattern is to throw away the scratch org and start over when you need clean data. Scratch orgs are cheap to create, which makes this practical for most teams.
Exercise
You run sf data import tree --plan data/Account-Contact-plan.json and the Accounts load, but the Contact step fails with an error about a required field. You fix Contacts.json and run the same command again against the same org. What problem will you have, and how would you avoid it?
Show answer
The first run already inserted the Accounts, and the import is not an upsert, so the second run inserts the same Accounts again and leaves duplicates. Before retrying, delete the Accounts from the first run, or discard the scratch org and create a new one. A repeatable approach is to add an external ID and use upsert, or to rebuild the scratch org each time.
Exercise
In a plan file, Contact is listed before Account, and the Account entry has saveRefs set to true. The data files use @AccountRef1 in Contacts. What happens, and what change fixes it?
Show answer
The Contact import runs before any Account IDs have been saved, so @AccountRef1 cannot be resolved and the Contact records fail. Move the Account entry ahead of the Contact entry so the parent loads first, and keep saveRefs: true on Account and resolveRefs: true on Contact.