Sub topic 9
Linking records with IDs and external IDs
This chapter assumes you can prepare a CSV file, map columns in Data Loader, and run an insert or update. It covers how to link imported records to related records, such as a Contact to its Account.
Why relationships need a key
A relationship in Salesforce is stored as a lookup or master-detail field that holds the ID of the parent record. When you import a child record, your CSV must tell Salesforce which parent to point to. You can do that in two ways:
- Salesforce ID. The 15- or 18-character ID that Salesforce assigns to every record. Use the 18-character form when you can, because it is case-insensitive and safe in spreadsheets.
- External ID. A custom field on the parent object that is marked as External ID. It holds a value from your source system, such as a customer number.
| Approach | Where the key comes from | Best when |
|---|---|---|
| Salesforce ID | Exported from the org | Parents already exist and you can export their IDs |
| External ID | Your source system | Parent and child come from the same system, or you load them in one run |
Linking by Salesforce ID
Export the parent records first. A query like this returns the IDs and a backup of current values:
SELECT Id, Name, Phone, Industry FROM Account WHERE Industry = 'Energy'Copy each parent ID into the child file. For Contacts, the column is AccountId:
FirstName,LastName,AccountId
Ana,Silva,001XXXXXXXXXXXXXXX
Ben,Okafor,001YYYYYYYYYYYYYYYReplace the placeholder IDs with real ones from your export. In the mapping step, map AccountId to the Account ID field on Contact.
A spreadsheet lookup such as VLOOKUP or XLOOKUP is the usual way to fill this column. Match on a value that is unique, such as the account name or a source-system number, and check for duplicate names first.
Linking by external ID
Linking by external ID avoids the lookup step. Suppose Account has a custom text field Customer_Number__c with External ID selected. Your Contact file can carry that value directly:
FirstName,LastName,Account.Customer_Number__c
Ana,Silva,C-1001
Ben,Okafor,C-1002The column header uses the relationship name, a dot, and the external ID field name. In Data Loader's mapping screen, the related object's external ID field appears under the relationship (for example, under Account). Map your column to it. Salesforce finds the Account whose Customer_Number__c is C-1001 and sets the lookup for you.
Note that in custom relationship fields, the relationship name ends in __r, not __c. For a custom lookup Region__c, the header is Region__r.Code__c.
Upsert with an external ID
An upsert updates a record if the key exists and inserts it if not. It needs a field to match on, which must be the Salesforce ID or an external ID field. Using an external ID makes a load repeatable: you can run the same file twice and get no duplicates.
In Data Loader, choose Upsert, select the object, and then pick the external ID field to match on. A file for Accounts might look like this:
Customer_Number__c,Name,Phone
C-1001,Acme Corp,555-0100
C-1002,Globex Ltd,555-0101Records whose Customer_Number__c already exists are updated. The rest are created.
Loading parents and children together
The order matters. Parents must exist before children point to them. A common sequence is:
- Upsert Accounts, with
Customer_Number__cset. - Upsert Contacts, with
Account.Customer_Number__cset. - Check the success and error files after each step.
Common errors
- Row fails with "no match found" or an invalid ID error. The external ID value does not exist on the parent, or the Salesforce ID has a typo or the wrong object prefix.
- Row fails with "duplicate external ID". More than one parent has the same external ID value. Make the field unique, or clean the data.
- IDs differ by case after saving in a spreadsheet. 15-character IDs are case-sensitive, and some tools change their case. Use 18-character IDs.
- Leading zeros or formats change. Spreadsheets can turn
00123into123. Format the column as text before saving. - Column not listed in the mapping. Check that the field is marked External ID, and that the header uses the correct relationship name.
Exercise
You have a Contact file with a column Account.Customer_Number__c, and some rows fail because the Account was not found. Name two causes you would check.
Show answer
First, check that an Account with that Customer_Number__c value exists in the org, and load Accounts before Contacts if it does not. Second, check that the values match exactly, including spaces, case rules, and leading zeros lost in the spreadsheet. Also confirm the field is marked External ID.
Exercise
Why is an upsert on an external ID safer to re-run than an insert?
Show answer
Upsert looks for an existing record with the same external ID. If one exists, it updates it instead of creating a duplicate. An insert always creates a new record, so running it twice creates duplicates.