Sub topic 6
Importing the dataset into a scratch org
This chapter covers loading an exported dataset into a fresh scratch org with sf data import tree. It assumes you have a Salesforce DX project, the sf CLI authorized to a Dev Hub, and a plan file with its data files from the previous chapters.
The working model
A scratch org starts with no records. It also has no custom objects or fields until you deploy your metadata. The import therefore has a fixed order:
- Create the scratch org.
- Deploy the project metadata, so every object and field in the data files exists.
- Import the data with the plan file.
If you reverse steps 2 and 3, the import fails because the target fields do not exist.
Run the import
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-scratchPass the plan file, not the individual data files. The --plan flag tells the CLI to process the entries in order. The plan imports Accounts.json first and keeps a map from each referenceId (such as AccountRef1) to the new Salesforce ID. It then imports Contacts.json and replaces each @AccountRef1 value with the real Account ID.
The old IDs from the source org are never reused. Salesforce assigns new IDs on insert, and the reference map is what keeps the relationships intact.
Verify the result
Query the org to confirm the parent and child records are linked:
sf data query \
--query "SELECT Name, (SELECT LastName FROM Contacts) FROM Account" \
--target-org my-scratchEach Account should list its Contacts. A Contact with no Account usually means the reference was not resolved.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `No such column` or `INVALID_FIELD` | Metadata was not deployed, or a field is missing from the project | Run `sf project deploy start` first |
| `REQUIRED_FIELD_MISSING` | A required field was not in the export query | Add the field to the query and export again |
| Contacts import with no Account | `resolveRefs` is `false`, or the `referenceId` does not match | Check the plan flags and the `@` reference names |
| `FIELD_CUSTOM_VALIDATION_EXCEPTION` | A validation rule rejects the seed records | Adjust the data, or deploy the rule after seeding |
Check your understanding
Exercise
You run sf data import tree --plan data/Account-Contact-plan.json against a new scratch org right after creating it, and the import fails with an invalid field error on a custom Account field. What did you skip, and how do you fix it?
Show answer
You skipped deploying the project metadata, so the custom field does not exist in the scratch org. Run sf project deploy start --target-org my-scratch, then run the import again. If the first import partly succeeded, delete the records it created or create a fresh scratch org first, to avoid duplicates.
Exercise
In the plan file, why must the Account entry set saveRefs to true and the Contact entry set resolveRefs to true?
Show answer
saveRefs on the Account entry stores the new Account IDs against their referenceIds. resolveRefs on the Contact entry makes the CLI replace values such as @AccountRef1 with those saved IDs. Without both settings, the Contacts cannot be linked to their Accounts.