Sub topic 9
Loading larger datasets with bulk commands
This chapter assumes you can export and import tree data with sf data export tree and sf data import tree, and that you know what an external ID field is. It covers when to switch to Bulk API commands and how to run a repeatable bulk upsert.
Why tree import stops being enough
The tree import commands send records through the sObject Tree API. That API accepts a limited number of records per request, and the files must be JSON with reference IDs. This works well for a few hundred records. It becomes slow and awkward for thousands of records, or when your source data is already a CSV file.
The Bulk API processes records asynchronously in batches. The CLI uploads a CSV file, Salesforce creates a job, and the job runs in the background. You can load far more records than with tree import. The trade-off is that CSV files have no reference IDs, so you must link related records another way.
| Need | Tree import | Bulk commands |
|---|---|---|
| Small dataset, a few objects | Good fit | More setup than needed |
| Thousands of records | Slow | Good fit |
| Relationships between objects | Reference IDs in plan file | External IDs |
| Input format | JSON | CSV |
| Run twice safely | Creates duplicates | Upsert updates existing rows |
Upserting with an external ID
An upsert inserts a record if no match exists and updates it if one does. The match is made on an external ID field, which is a custom field marked as External ID on the object. Because the match uses your own key, running the load twice does not create duplicates.
Here is a small CSV with an external ID column named External_Id__c:
External_Id__c,Name,Industry
ACME-001,Acme Corp,Technology
ACME-002,Globex,Technology
ACME-003,Initech,FinanceLoad it with the bulk upsert command:
sf data upsert bulk \
--sobject Account \
--file data/accounts.csv \
--external-id External_Id__c \
--target-org my-scratch \
--wait 10The --wait 10 option makes the command wait up to 10 minutes for the job to finish and then print the result. Without it, the command returns right away with a job ID, and you check the job later.
sf data upsert resume --job-id 750xx0000000001AAAReplace the job ID with the one the first command printed.
Linking child records
A Contact CSV cannot use @AccountRef1 references. Instead, Bulk API lets a child row point at its parent through the parent's external ID. Use a column header of the form Relationship__r.External_Id__c. For the standard Account lookup on Contact, the header is Account.External_Id__c:
External_Id__c,FirstName,LastName,Account.External_Id__c
C-001,Ada,Lovelace,ACME-001
C-002,Grace,Hopper,ACME-002Load Accounts first, then Contacts, using the same command with --sobject Contact. The order matters because the parent must exist when the child job runs.
Common errors
- Field not found on the external ID. The field must exist in the target org and be marked External ID. Deploy your project metadata before loading.
- Unresolved parent. The parent External ID in the child CSV does not match any loaded Account. Check for typos and for load order.
- Partial failures. A job can finish with some rows failed. Check the failed results of the job rather than assuming success.
- Validation rules and triggers. Bulk loads still run them. A failing rule rejects the affected rows.
Exercise
You run the same sf data upsert bulk command twice with the same CSV. What is the result, and why?
Show answer
The second run updates the existing records and creates no duplicates, because each row is matched on the external ID field (External_Id__c). A plain insert would create a second copy of every row.