Sub topic 9
Linking Records with IDs and External IDs
When you import child records such as Contacts, Opportunities, or Cases, each one has to point to a parent record. This chapter covers the three ways to do that with Data Loader: the Salesforce ID, an external ID, and the polymorphic or name-based shortcuts that the Data Import Wizard offers. It assumes you know how to prepare a CSV file, how Data Loader maps columns to fields, and what insert, update, and upsert do.
How a relationship is stored
A lookup or master-detail field on a child object holds the 15- or 18-character Salesforce ID of the parent. For example, Contact.AccountId holds the ID of an Account. During an import, your CSV has to give Salesforce enough information to find that ID. There are two ways to do it:
- Supply the parent's Salesforce ID directly in a column mapped to the relationship field (
AccountId). - Supply the parent's external ID value in a column mapped to the relationship reference (
Account:External_Id__c), and let Salesforce look up the ID for you.
The second way is what makes large, multi-object loads practical. You do not have to load parents, export their new IDs, and merge those IDs into the child file.
Option 1: Salesforce IDs
Salesforce IDs come in two forms.
| Form | Length | Case-sensitive | Notes |
|---|---|---|---|
| 15-character | 15 | Yes | The form shown in most URLs. Spreadsheets that ignore case can corrupt or merge values. |
| 18-character | 18 | No | The last three characters are a checksum for case. Safer in Excel and in reports. |
Data Loader accepts both forms. Use the 18-character form in CSV files, because a spreadsheet's case-insensitive functions such as VLOOKUP can match the wrong record when two 15-character IDs differ only by case.
A typical workflow for ID-based linking:
- Load or identify the parent records.
- Export them with Data Loader, including
Idand a natural key such asName. - Add the parent ID to the child CSV, for example with a lookup formula in the spreadsheet.
- Map the column to the relationship field and run the insert.
This works, but it has two weak points. The lookup step is manual and easy to get wrong. It also forces you to finish the parent load before you can build the child file.
Here is a child file that links Contacts to existing Accounts by ID.
FirstName,LastName,Email,AccountId
Ana,Lopez,[email protected],001XXXXXXXXXXXXXXX
Ben,Okafor,[email protected],001YYYYYYYYYYYYYYYThe IDs are placeholders. Replace them with real IDs from an export, such as the SOQL query in the earlier chapters.
Option 2: External IDs
An external ID is a custom field marked External ID in its definition. It usually stores the key from the system the data came from, such as an ERP customer number. Supported field types are text, number, email, and auto number. Marking a field as an external ID also indexes it, so lookups on it are fast.
Marking the field Unique adds a further guarantee. Salesforce will reject two records with the same value, which prevents duplicates and makes the upsert result predictable.
External IDs give you two capabilities:
- Upsert. Data Loader matches each row on the external ID. If a record has that value, it is updated. If none does, a new record is created.
- Relationship lookups. A child row can name its parent by external ID instead of by Salesforce ID.
Upsert on an external ID
For an upsert, Data Loader asks which field to match on. The choices are Id or any field marked as an external ID on that object. Here is an Account file keyed on a custom field ERP_Id__c.
ERP_Id__c,Name,Industry
C-1001,Acme Corp,Manufacturing
C-1002,Globex Ltd,TechnologyRunning an upsert on ERP_Id__c makes the following happen for each row:
- No match: Salesforce inserts a new Account.
- One match: Salesforce updates that Account.
- More than one match: the row fails with a duplicate external ID error. This can happen if the field is not marked Unique.
Because the same file can be run again with no harm, upsert on an external ID is a good fit for repeated loads from another system.
Linking children by external ID
To link a Contact to an Account using the Account's external ID, Data Loader needs to know three things: the child field, the parent object, and the parent's external ID field. The mapping dialog shows these under the lookup relationship, for example Account with the child choice ERPId_c. In the CSV, the header can be any name you like, because you map it yourself. In command-line and API contexts the same reference is written Account:ERP_Id__c.
FirstName,LastName,Email,ERP_Account_Ref
Ana,Lopez,[email protected],C-1001
Ben,Okafor,[email protected],C-1002In the mapping step, map ERP_Account_Ref to the relationship Account (ERPId_c) and not to AccountId. Salesforce looks up the Account whose ERP_Id__c equals C-1001 and stores its ID in the Contact.
A few rules apply:
- The parent record must already exist, or be loaded in an earlier run. Salesforce does not create parents from a child row.
- If no parent matches the value, the row fails. If the lookup is optional and you leave the cell blank, the relationship is left empty.
- The external ID field is on the parent object. A separate external ID on the child object is used only when you upsert the child.
- Matching on text external IDs is case-insensitive by default unless the field is defined as case-sensitive. Plan for this when your source system distinguishes
abcfromABC.
Child and parent in one upsert
You can combine both capabilities. A Contact upsert can match on the Contact's own external ID and set its Account by the Account's external ID in the same row.
Contact_Ext__c,LastName,Email,ERP_Account_Ref
K-501,Lopez,[email protected],C-1001
K-502,Okafor,[email protected],C-1002Here Contact_Ext__c is the match field for the upsert, and ERP_Account_Ref is mapped to the Account relationship by ERP_Id__c.
Load order
Since a child needs an existing parent, load in dependency order. The diagram shows a typical sequence.
Each arrow means that the earlier load must finish, and its errors be resolved, before the next one starts. If you load children while some parents failed, the child rows that point to them also fail.
Choosing between IDs and external IDs
| Criterion | Salesforce ID | External ID |
|---|---|---|
| Needs a field set up in advance | No | Yes, an administrator must create the field |
| Needs a lookup step in the spreadsheet | Yes | No |
| Works for records created inside Salesforce | Yes | Only if the field is populated |
| Safe to re-run the same file | Update only | Yes, with upsert |
| Stays meaningful after a sandbox refresh | No, IDs differ between orgs | Yes, the source key is the same |
The last row matters in practice. A Salesforce ID from production does not exist in a sandbox copy that was created before those records existed, and the IDs of records created separately in each org never match. External IDs travel between orgs because they come from your own data.
Relationships in the Data Import Wizard
The wizard handles relationships in a limited way. For Accounts and Contacts, it can match a Contact to an Account by name or by ID, and it can match on an external ID for custom objects that support the wizard. It does not offer the full set of lookup options that Data Loader does. If you need to link Opportunities to Accounts, or any object the wizard does not support, use Data Loader.
Common errors
| Error message (typical) | Likely cause | Fix |
|---|---|---|
| `INVALID_CROSS_REFERENCE_KEY: invalid cross reference id` | The Salesforce ID does not exist in this org, or points to the wrong object type | Check that the ID came from this org and that its three-character prefix matches the parent object. Accounts start with `001`. |
| `Foreign key external ID not found for field ... in entity ...` | No parent has that external ID value | Load the parent first, and check for extra spaces or changed formatting in the CSV. |
| `DUPLICATE_VALUE` or `Duplicate external ID` | More than one record shares the external ID value, or the file repeats a value | Make the field Unique and remove repeated keys from the file. |
| `MALFORMED_ID` | The cell is not a valid 15- or 18-character ID | Check for truncation, such as a spreadsheet converting the value, or leading and trailing spaces. |
| `INVALID_FIELD ... Account:ERP_Id__c` | The relationship reference is spelled wrongly or the field is not an external ID | Check the API names and confirm the External ID option is checked. |
Pitfalls to check before you run
- Leading zeros and number formatting. An external ID such as
00123becomes123if a spreadsheet treats it as a number. Format the column as text before you paste values in. - Hidden whitespace. Values copied from another system often have trailing spaces. Trim the column.
- Duplicates in the source file. Two rows with the same external ID in one upsert can fail or overwrite each other in the same batch.
- Blank match values. A blank external ID cell on an upsert row creates a record with no key, and you lose the ability to match it later.
- Parent changes on update. Updating a Contact's
AccountIdmoves it to another Account. In a master-detail relationship, the child can be reparented only if the field allows it.
Verifying the result
After the run, open the success file. For an upsert, it contains a Created column that says whether each row inserted or updated a record. Spot-check a few child records in Salesforce and confirm that each one shows the right parent. You can also count orphans with a query such as this one.
SELECT COUNT() FROM Contact WHERE AccountId = null AND Email LIKE '%@acme.example'A count above zero for a group that should all have parents points to rows where the lookup value was blank or failed.
Exercise
You are loading 5,000 Contacts from an ERP system. Each row has the ERP customer number of its Account. The Accounts were loaded last week with a unique external ID field ERP_Id__c. Which approach do you use to link each Contact to its Account, and what do you map the customer number column to?
Show answer
Use Data Loader with an insert (or an upsert if the Contacts also have their own external ID). Map the customer number column to the Account relationship using the parent's external ID field, shown as Account (ERPIdc), not to AccountId. Salesforce looks up each Account by its ERPId__c value, so no export or spreadsheet lookup of Salesforce IDs is needed.
Exercise
An upsert on ERP_Id__c reports a duplicate external ID error for several rows, although the file looks correct. Name two possible causes.
Show answer
The field is not marked Unique, so more than one existing Account already has that value and the match is ambiguous. Or the same key appears more than once in the file, for example because of repeated rows or because trailing spaces and case differences make values that look different match the same record.