Sub topic 12
Preserving Relationships and Planning Restore Order
This chapter assumes you can write basic SOQL, run sf data query, and know what lookup and master-detail fields are. A CSV backup is only useful if you can load it back with its relationships intact. That depends on two things you decide at export time: which Ids you keep, and in what order you load the objects.
How Salesforce stores relationships
A relationship is a field that holds the 15 or 18 character Id of another record. A Contact stores its Account in AccountId. A Task stores its related record in WhatId and its person in WhoId.
Salesforce generates a new Id for every inserted record. When you restore into the same org after a deletion, or into a sandbox, the restored records get new Ids. Any child file that still holds the old parent Id will point at nothing.
Export the keys, not only the names
Always export Id and every lookup field for the object. Parent names are not enough, because names are not unique.
SELECT Id, FirstName, LastName, AccountId, Account.Name, OwnerId
FROM ContactExport child objects with their own queries. Flat CSV tools cannot export subquery results, so do not rely on a (SELECT ... FROM Tasks) subquery for the backup itself.
Use external Ids to remap
To restore, load parents first and record the new Ids. Then replace the old parent Ids in the child files. The simplest way to do this is an external Id field.
- Create a custom field on each object, for example
Legacy_Id__c, and mark it External ID. - Before restoring, load the old Salesforce Id into that field with each parent record.
- Upsert children using the parent's external Id in a relationship column such as
Account.Legacy_Id__c. Salesforce looks up the parent for you.
This avoids manual lookups in a spreadsheet and works for any number of rows.
Plan the restore order
Load parents before children. A child row that references a missing parent fails with a FIELD_INTEGRITY_EXCEPTION or a required-field error.
Check these points when you build your own order:
- Master-detail children cannot exist without a parent, so the parent must load first.
- Self-lookups, such as
Account.ParentId, need two passes. Load all rows with the field empty, then update the field. - Owners must exist as active users. Map
OwnerIdto a valid user, or the load fails. - Triggers, flows, and validation rules fire during a restore. Disable or bypass them for the load, then turn them back on.
- Fields such as
CreatedDatecan only be set if Set Audit Fields upon Record Creation is enabled for your org.
Exercise
You export Account and Contact to separate CSV files. After a restore into a sandbox, every Contact fails to load with a field integrity error on AccountId. What went wrong, and how do you fix it?
Show answer
The Contacts still hold the old production Account Ids, which do not exist in the sandbox. Load Accounts first with their old Id stored in an external Id field such as Legacy_Id__c, then upsert Contacts using Account.Legacy_Id__c as the parent reference, so Salesforce resolves the new Id for each row.