Sub topic 8
External IDs and upsert for repeatable loads
This chapter assumes you can run sf commands against a scratch org, know what a lookup relationship is, and have read the chapters on tree export and import. You should also be able to edit a CSV file.
Why plain inserts are not repeatable
An insert always creates new records. If you run the same import twice, you get two copies of every row. Salesforce generates a new 18-character record ID each time, and nothing in the file tells the org that "Acme" already exists.
An external ID fixes this. It is a custom field that you mark as an External ID, and it holds a key from your own data, such as ACC-001. An upsert looks for a record whose external ID matches the incoming row. If it finds one, it updates that record. If it finds none, it inserts a new record.
Creating the external ID field
Add a custom text field to the object in your DX project and deploy it before loading data. Mark it External ID and Unique, so the org rejects duplicate keys.
<?xml version="1.0" encoding="UTF-8"?>
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>External_Id__c</fullName>
<label>External Id</label>
<type>Text</type>
<length>50</length>
<externalId>true</externalId>
<unique>true</unique>
<caseSensitive>true</caseSensitive>
</CustomField>Save it as force-app/main/default/objects/Account/fields/External_Id__c.field-meta.xml, then run sf project deploy start.
Upserting from a CSV file
Create data/accounts.csv. The header must use field API names, and the External_Id__c column holds your keys.
External_Id__c,Name,Industry
ACC-001,Acme Corp,Technology
ACC-002,Globex,TechnologyThen upsert it.
sf data upsert bulk \
--sobject Account \
--file data/accounts.csv \
--external-id External_Id__c \
--target-org my-scratch \
--wait 10The command uses the Bulk API. --wait 10 makes the CLI wait up to 10 minutes for the job to finish. Run it a second time and the job reports updates, not inserts. The row count in the org stays the same.
Linking child records with external IDs
Tree import resolves relationships through reference IDs inside one plan. With CSV loads you often have no such plan. Instead, a child file can point at its parent by the parent's external ID. In a Bulk API CSV, a relationship column uses the form Relationship.ExternalIdField.
External_Id__c,FirstName,LastName,Account.External_Id__c
CON-001,Ada,Lovelace,ACC-001
CON-002,Grace,Hopper,ACC-002The Account.External_Id__c column tells Salesforce to find the Account with that key and set AccountId. You need no Salesforce IDs in the file. This lets you load parents and children in separate runs, and the data stays portable between orgs. Give Contact its own External_Id__c field too, so the Contact load is also repeatable.
sf data upsert bulk \
--sobject Contact \
--file data/contacts.csv \
--external-id External_Id__c \
--target-org my-scratch \
--wait 10Always load parents first. A child row whose parent key does not exist fails.
Common errors
| Symptom | Likely cause |
|---|---|
| Duplicate rows after a rerun | The field is not marked External ID, or you used `import` instead of `upsert` |
| Column not found | The header does not match the API name, including the `__c` suffix |
| Child row fails to find parent | The parent was not loaded yet, or its key differs in case or spacing |
| Duplicate external ID value error | Two rows in the file share the same key |
Exercise
You run sf data upsert bulk on accounts.csv twice. The second run creates duplicates. Name two things to check.
Show answer
Check that the field named in --external-id is marked External ID (and Unique) in the org, and that the CSV key column holds the same values on both runs, with the same spelling, case, and spacing. Also confirm you ran an upsert command and not an insert or tree import.