Engineering · Data Migration · Salesforce Architecture
Who Goes First? Circular Lookups and the Links That Must Never Wait
SmartSeed copies related records from one Salesforce org to another: an Account with its Contacts, Cases, Opportunities and everything they point to. To rebuild those records in the target org, it has to create them in the right order. Parents first, then the records that point to them.
Most of the time that order is obvious. Sometimes it doesn't exist.
The problem: two records that point at each other
A lookup is a link from one record to another. A Contact looks up its Account, so you create the Account first and the Contact second. Easy.
Now picture an Account that names its main Contract, while the Contract belongs to that Account. Each one points at the other. Whichever you create first, its link points to a record that doesn't exist yet. There is no correct order.
So one link has to start empty and be filled in later. The only real question is: which one?

Two records that point at each other: one link has to wait.
What went wrong first
Our first version answered that question without really asking it. SmartSeed searches through the related records to work out the order, and it left empty whichever link the search happened to meet first.
In a test run in our own sandboxes, that was the link from Case to Account. The target org created 10,194 Cases without their Account, and every record that required one failed.
Nothing was wrong with the search. It just isn't a decision. The order a search visits records in is an accident, and we had let an accident choose which data to give up.
The fix: must-have links and can-wait links
We sorted every link into one of two kinds:
- Must-have links are the ones Salesforce requires (in developer terms: lookups that can't be empty). They are never left empty. They decide the order.
- Can-wait links are optional. Only these may start empty, only when a loop leaves no other choice, and as few of them as possible.
Then the run works in three steps:
- Create records in the right order, with must-have links honoured every time.
- Note down every link that had to wait. SmartSeed records only the links that genuinely had to wait: the source record had a value, the parent wasn't created yet, and that parent is part of the same transfer.
- Fill them in at the end, once every record exists. Failures are counted per record and reported, never retried forever.
If your data has no loops, nothing waits and there's nothing to repair.

Must-have links decide the order. Can-wait links are noted down and filled in at the end.
A bonus lesson: the clever shortcut
Later, we tried a "smarter" way to work out the order: sorting objects by the direction of their links instead of walking through the records. It put a required parent ahead of the record it points to, and the target org's own validation rules rejected it.
We went back to walking through the records, and added tests that use a realistic data model, so the next clever idea has to survive real-looking data first.
What we'd tell you
- Decide what must never break, and what can wait. Before the data moves, not after.
- "Whatever came first" is not a decision. The order a search happens to find things in isn't a design.
- Write down every compromise. So it can be fixed, and reported, at the end.

Three rules for any data move.

SmartSeed in action. (Screenshot to come.)
If you'd like to see SmartSeed for yourself, start at smartseed.app.
And we'd like to hear from you: what's the nastiest circular reference you've had to migrate?