Sub topic 5
Plan files and reference IDs: keeping relationships intact
This chapter assumes you can read JSON, know lookup relationships between Salesforce objects, and have run sf data export tree from the previous chapter. It explains how a plan file lets sf data import tree load related records in the right order and connect them.
The problem: IDs change on import
A Contact stores its parent in AccountId. The source org's Account IDs mean nothing in a new org, because Salesforce assigns fresh IDs on insert. If you copied the old AccountId values into the Contact file, the import would fail or point at nothing.
The tree format avoids this with reference IDs. Each record carries a local label. A child record points to its parent by that label. During import, the CLI swaps each label for the real ID of the record it just created.
How the pieces fit together
A plan file is a JSON array. Each entry describes one batch of records: the object type, the data files, and two flags.
saveRefs: truetells the CLI to remember the new ID of every record in this batch, keyed by itsreferenceId.resolveRefs: truetells the CLI to replace@-prefixed values in this batch with the remembered IDs.
[
{
"sobject": "Account",
"saveRefs": true,
"resolveRefs": false,
"files": ["Accounts.json"]
},
{
"sobject": "Contact",
"saveRefs": false,
"resolveRefs": true,
"files": ["Contacts.json"]
}
]The CLI processes entries from top to bottom. Accounts go first and their IDs are saved. Contacts go second and their references are resolved.
Reference IDs in a data file
Each record declares its own label in attributes.referenceId. A child uses @ plus the parent's label.
{
"records": [
{
"attributes": { "type": "Contact", "referenceId": "ContactRef1" },
"FirstName": "Ada",
"LastName": "Lovelace",
"Email": "[email protected]",
"AccountId": "@AccountRef1"
}
]
}For this to work, Accounts.json must contain a record whose referenceId is AccountRef1. Labels are plain text matched exactly, and they must be unique across the plan.
Generating the plan automatically
Adding --plan to the export command writes the data files and a plan file together. The CLI creates the labels and the @ references for you, and it orders the entries parent first.
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-orgCommon errors
| Symptom | Likely cause |
|---|---|
| Contact import fails with an invalid ID or unresolved reference | The Account entry lacks `saveRefs: true`, or the Contact entry lacks `resolveRefs: true` |
| Reference not found | The `@` label doesn't exactly match a parent `referenceId`, including case |
| Parent not found even though labels match | The child entry comes before the parent in the plan |
| Duplicate reference ID error | Two records share a label |
Exercise
You add Opportunity records that reference Accounts using "AccountId": "@AccountRef1". Where should the Opportunity entry go in the plan, and which flags does it need if nothing references Opportunities?
Show answer
Place it after the Account entry, since Accounts must be inserted and their IDs saved first. Set resolveRefs to true so @AccountRef1 is replaced. saveRefs can be false because nothing refers to Opportunities.