Sub topic 5
Plan files and reference IDs: keeping relationships intact
This chapter assumes you can run sf data export tree and sf data import tree from the previous two chapters. It also assumes you know lookup and master-detail relationships and can read JSON. Here you will learn how a plan file lets the CLI import several objects while keeping their relationships intact.
The problem: IDs change on import
Every record has a Salesforce ID, and that ID belongs to one org. When you import an Account into a scratch org, the scratch org assigns it a new ID. A Contact exported from the source org still carries AccountId with the old source ID. The old ID points to nothing in the target org, so the import fails or links to the wrong record.
The tree commands solve this with placeholders called reference IDs. The data file does not store real Salesforce IDs for relationships. Each record gets a label, and other records point at that label. During import, the CLI swaps each label for the real ID the target org just created.
Reference IDs in a data file
Each record in a data file has an attributes block with a type and a referenceId. The referenceId is the record's own label. To point at another record, use the same label with an @ prefix in the lookup field.
{
"records": [
{
"attributes": { "type": "Account", "referenceId": "AccountRef1" },
"Name": "Analytical Engines Ltd",
"Industry": "Technology"
},
{
"attributes": { "type": "Account", "referenceId": "AccountRef2" },
"Name": "Difference Systems",
"Industry": "Technology"
}
]
}Save that as data/Accounts.json. Now a Contact file in data/Contacts.json:
{
"records": [
{
"attributes": { "type": "Contact", "referenceId": "ContactRef1" },
"FirstName": "Ada",
"LastName": "Lovelace",
"Email": "[email protected]",
"AccountId": "@AccountRef1"
},
{
"attributes": { "type": "Contact", "referenceId": "ContactRef2" },
"FirstName": "Charles",
"LastName": "Babbage",
"Email": "[email protected]",
"AccountId": "@AccountRef2"
}
]
}The value @AccountRef1 is not an ID. It means "the Account that was imported with the reference ID AccountRef1".
The plan file
A single file can hold a parent and its children as nested records. That approach is limited: the export only nests one level, and the tree API accepts at most 200 records per request. A plan file splits the dataset into separate files per object and tells the CLI how to handle references for each.
[
{
"sobject": "Account",
"saveRefs": true,
"resolveRefs": false,
"files": ["Accounts.json"]
},
{
"sobject": "Contact",
"saveRefs": false,
"resolveRefs": true,
"files": ["Contacts.json"]
}
]Save it as data/Account-Contact-plan.json. The plan is an array of steps, and the CLI runs the steps from top to bottom.
| Property | Meaning |
|---|---|
| `sobject` | The object type for every record in the listed files. |
| `files` | Data files for this step, relative to the plan file's folder. |
| `saveRefs` | After inserting, remember the mapping from each `referenceId` to the new Salesforce ID. |
| `resolveRefs` | Before inserting, replace each `@Label` value with the saved ID. |
Set saveRefs: true on any step whose records are the target of a later reference. Set resolveRefs: true on any step whose records contain @ references. A step can be both, which is common for a middle object such as Opportunity (it references Accounts and is referenced by Opportunity Line Items).
The diagram below shows the flow.
Run the import against your scratch org:
sf data import tree \
--plan data/Account-Contact-plan.json \
--target-org my-scratchOn success the CLI prints a table of the records it created, showing each reference ID and the new Salesforce ID. Keep that output when debugging, because it is the only place the label-to-ID mapping is visible.
What the export generates
When you export with --plan, the CLI writes the data files and the plan for you. It assigns reference IDs such as AccountRef1 and ContactRef1 automatically and writes the @ references into child records.
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-orgTreat the generated files as a starting point. Open the plan and check three things: the order of steps, the saveRefs and resolveRefs flags, and the file names. Then commit the folder so the dataset is versioned like code.
Ordering rules
The CLI resolves references in a single forward pass, so order matters in two places.
- Between steps. A step can only use labels saved by an earlier step. If Contacts come before Accounts in the plan,
@AccountRef1has no saved value and the import fails with an unresolved reference error. - Within a file. Records are inserted in file order. If records in the same file point at each other (for example a parent Account via
ParentId), the parent must appear earlier in the file. Give that step bothsaveRefs: trueandresolveRefs: true.
For master-detail and required lookups, the parent must exist before the child, so build the plan from the top of your dependency tree down. A reasonable order for a sales dataset is Account, Contact, Opportunity, then Opportunity Line Item. Objects that reference Pricebook entries need those records in an earlier step too, or need standard Pricebook IDs looked up another way.
Rules for reference IDs
- Each
referenceIdmust be unique across the whole plan, not only in its file. Two records both labeledRef1cause a conflict. - Use letters and digits. Avoid spaces and punctuation, because the label also appears after
@. - The label is only a label. It is never written to Salesforce, so you can rename it freely as long as every
@reference is updated to match. - The
@reference must be the complete field value."@AccountRef1"works. Text such as"Account @AccountRef1"does not. - The
Idfield is dropped on import. Do not try to set an ID by hand.
Splitting large files
The tree API accepts at most 200 records per request. If you need more, split one object into several files and list them all in one step:
[
{
"sobject": "Account",
"saveRefs": true,
"resolveRefs": false,
"files": ["Accounts-1.json", "Accounts-2.json"]
}
]Reference IDs saved from both files stay available to later steps. If you are regularly past a few hundred records, switch to the bulk commands covered in a later chapter.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Error about an unresolved or invalid reference such as `@AccountRef1` | The parent step runs later, lacks `saveRefs: true`, or the label is misspelled | Move the parent step earlier, set `saveRefs: true`, and match the label exactly |
| Child records are created with a blank lookup | The child step has `resolveRefs: false`, or the lookup field was left out of the data file | Set `resolveRefs: true` and confirm the field contains `@Label` |
| Duplicate reference ID error | Two records use the same `referenceId` | Rename one and update its references |
| Import stops with a required field or validation error | The object or field is missing in the org, or a rule rejects the data | Deploy metadata first and fix the data (covered in the troubleshooting chapter) |
| Old source IDs appear in the data | A lookup holds a raw ID instead of `@Label` | Replace it with a reference to a record in the plan |
Exercise
You export Accounts, Contacts, and Opportunities. Opportunities reference Accounts through AccountId, and Opportunity Contact Roles reference both Opportunities and Contacts. Describe the plan step order and which steps need saveRefs and resolveRefs set to true.
Show answer
Order the steps Account, Contact, Opportunity, OpportunityContactRole. Account needs saveRefs true and resolveRefs false, because nothing is referenced before it. Contact needs saveRefs true (the roles point at it) and resolveRefs true (it points at Accounts). Opportunity needs both true, because it points at Accounts and the roles point at it. OpportunityContactRole needs resolveRefs true and saveRefs false, because it only references earlier records. If any step ran before the step that saves its target labels, the import would fail with an unresolved reference.
Exercise
A teammate's Contacts file contains "AccountId": "001xx000003DGbQAAW", copied from the source org. The import succeeds but the Contacts have no Account in the scratch org, or it fails with an invalid ID error. Why, and how do you fix it?
Show answer
The ID belongs to the source org and does not exist in the scratch org, so it cannot link to anything there. Replace the value with an @ reference such as "@AccountRef1", make sure the Account record in the Accounts file has that referenceId, put the Account step first with saveRefs true, and set resolveRefs true on the Contact step.