Sub topic 10
Upserting with an external ID field
This chapter assumes you can already create an ingest job, upload CSV data, and close the job with UploadComplete. It also assumes you know what an external ID field is. Here you will change that workflow so it updates existing records and inserts new ones in a single job.
What upsert does
An upsert compares each CSV row to existing records by the value of an external ID field. Salesforce then does one of three things:
| Matches found | Result |
|---|---|
| None | The row is inserted as a new record. |
| One | The existing record is updated. |
| More than one | The row fails with an error. |
Upsert lets you rerun the same load without creating duplicates. It also means you don't need to know Salesforce record IDs, which is useful when the data comes from another system.
Requirements
- The field must be marked as an external ID on the object. A custom field such as
Legacy_Id__cqualifies only if the External ID option is enabled. - The CSV header must include that field's API name as a column.
- The job must name the field in
externalIdFieldName.
A matching CSV looks like this:
Legacy_Id__c,FirstName,LastName,Email
C-1001,Ada,Lovelace,[email protected]
C-1002,Alan,Turing,[email protected]Creating the upsert job
Two properties differ from an insert job. operation is upsert, and externalIdFieldName names the matching field.
curl -X POST "$INSTANCE_URL/services/data/v62.0/jobs/ingest" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"object":"Contact","operation":"upsert","externalIdFieldName":"Legacy_Id__c","contentType":"CSV","lineEnding":"LF"}'The upload (PUT .../batches), the close (PATCH with UploadComplete), and the monitoring steps are unchanged. Reuse the commands from earlier chapters.
Telling inserts from updates
The successful results file has an sf__Created column. A value of true means the row was inserted. false means an existing record was updated. Fetch the file once the job reaches JobComplete:
curl "$INSTANCE_URL/services/data/v62.0/jobs/ingest/$JOB_ID/successfulResults/" \
-H "Authorization: Bearer $ACCESS_TOKEN"Counting these values is a quick way to check that a load did what you expected. If you expected only updates and see many true values, your external ID values probably don't match the data in the org.
Common errors
- The field is not an external ID. The job is rejected or its rows fail. Check the field's External ID setting in Setup.
- The external ID column is missing or misspelled in the header. Every row fails, because Salesforce can't match anything.
- Blank external ID values. A blank value can't be used for matching, so those rows fail. Filter them out before upload.
- Duplicate values in the org. If the field isn't unique and two records share a value, the row fails with a multiple-match error. Mark the field Unique, or clean the data first.
- Duplicate values in the same file. Several rows carrying the same external ID in one job can produce unpredictable results or lock errors. Deduplicate the file before upload.
Case sensitivity of matching follows the field's settings. If the field is not case sensitive, c-1001 and C-1001 are treated as the same value.
Failed rows appear in failedResults/ with sf__Error, as with any ingest job. You can fix them and resubmit them in a new upsert job.
Exercise
You run an upsert job on Contact with externalIdFieldName set to LegacyIdc. The successful results show sf_Created as false for every row. What does that tell you, and which two things would you check if you expected new records?
Show answer
Every row matched an existing Contact, so all rows were updates and none were inserted. If you expected new records, check that the LegacyId_c values in the CSV are genuinely new, and that the CSV does not contain IDs that already exist in the org because of an earlier load.