Sub topic 9
Loading larger datasets with bulk commands
This chapter covers the sf data bulk commands. They load CSV files through the Bulk API, which handles volumes that the tree commands do not suit.
Prerequisites
This chapter assumes you have these:
- The Salesforce CLI (
sf) is installed and authorized to a scratch org or sandbox. - You know what an external ID field is. The previous chapter covered external IDs and upsert.
- You can read and edit CSV files.
- The project metadata is already deployed, so the objects and fields exist in the target org.
When tree import stops being the right tool
sf data import tree reads JSON files and sends records through the sObject Tree API. It keeps parent and child relationships intact through reference IDs. It also limits each request to a small number of records, so it suits small, related datasets of a few hundred records.
The Bulk API is built for volume. It takes a CSV file, splits it into batches, and processes them asynchronously on the server. The CLI uploads the file and then polls the job until it finishes. You pay for that scale in one way: the CSV has no reference ID mechanism. Each file loads one object, and you must express relationships through IDs or external IDs yourself.
| Question | Tree import | Bulk commands |
|---|---|---|
| File format | JSON plus a plan file | CSV, one object per file |
| Typical volume | Tens to hundreds of records | Thousands to millions of records |
| Parent-child links | `referenceId` and `@` references | External ID columns or real record IDs |
| Processing | Synchronous request | Asynchronous job on the server |
| Repeatable loads | Not by itself | Yes, with upsert and an external ID |
The bulk command family
The CLI offers one command per operation. Each takes --sobject, --file, and --target-org.
| Command | What it does | Needs |
|---|---|---|
| `sf data import bulk` | Inserts records | CSV with field API names as headers |
| `sf data upsert bulk` | Inserts new rows and updates existing ones | `--external-id` field |
| `sf data update bulk` | Updates existing records | `Id` column |
| `sf data delete bulk` | Deletes records | `Id` column |
Use sf data upsert bulk for seed data. A seed script is run many times, and upsert is the operation that tolerates that.
Run sf data upsert bulk --help on your installed version. Flag names and defaults have changed across CLI releases, and older guides may show the legacy sfdx force:data:bulk:upsert form.
Preparing the CSV
The header row holds field API names. Each later row is one record. A file for the Account example looks like this:
External_Id__c,Name,Industry,BillingCity
ACC-0001,Northwind Traders,Technology,Seattle
ACC-0002,Contoso Labs,Technology,Austin
ACC-0003,Fabrikam Systems,Technology,DenverThree rules prevent most failures:
- Header names must match field API names exactly, including the
__csuffix on custom fields. - Include every field that is required by the object or by a validation rule.
- Save the file as UTF-8 and quote any value that contains a comma or a line break.
Dates use YYYY-MM-DD and date-times use ISO 8601, for example 2026-12-01T09:24:00.000Z. Picklist values must match an active value unless the field allows any value.
Running an upsert
Make sure the external ID field exists in the org. It must be marked External ID, and marking it Unique stops duplicates from being created by a race between two loads.
sf data upsert bulk \
--sobject Account \
--file data/accounts.csv \
--external-id External_Id__c \
--target-org my-scratch \
--wait 10The --wait 10 flag makes the CLI wait up to ten minutes for the job to finish and then report the result. Without --wait, the command returns after submitting the job. You can then check on it later using the job ID the command prints.
Run the same command a second time. The CLI reports the same number of processed records, but Salesforce now matches each row on External_Id__c and updates the existing Account instead of creating another one. That behavior is what makes a script safe to repeat.
Loading related records with external IDs
The Contact file needs to point at Accounts. You do not know the Account record IDs in advance, because each org assigns its own. An upsert on the Contact can use an external ID to find the parent. In Bulk API 2.0 CSV, a relationship column header takes the form Relationship.ExternalIdField:
External_Id__c,FirstName,LastName,Email,Account.External_Id__c
CON-0001,Ada,Lovelace,[email protected],ACC-0001
CON-0002,Grace,Hopper,[email protected],ACC-0001
CON-0003,Alan,Turing,[email protected],ACC-0002The last column header, Account.External_Id__c, tells Salesforce to look up the Account whose External_Id__c equals the cell value and to set AccountId from it. This replaces the @AccountRef1 reference used in tree files.
This needs External_Id__c on both Account and Contact. Load in dependency order, as you did with the plan file: parents first, then children.
sf data upsert bulk --sobject Account --file data/accounts.csv \
--external-id External_Id__c --target-org my-scratch --wait 10
sf data upsert bulk --sobject Contact --file data/contacts.csv \
--external-id External_Id__c --target-org my-scratch --wait 10A parent row that does not exist produces a row-level error for that child, not a failure of the whole job.
How a bulk job runs
Because processing is asynchronous, a job can partly succeed. Records in different batches commit independently, and a failure in one row does not roll back the rest. Check the failed count every time, not only the exit code of a script.
Reading results and failures
When a job finishes with failed records, you can ask the CLI for details by job ID:
sf data bulk results --job-id 750xx0000000001AAA --target-org my-scratchThe results include a failed-records file with the original columns plus an error message per row. Typical messages and their meaning:
| Message | Likely cause |
|---|---|
| `REQUIRED_FIELD_MISSING` | A required field has no value in that row |
| `INVALID_FIELD` or `No such column` | A header does not match a field API name |
| `FIELD_CUSTOM_VALIDATION_EXCEPTION` | A validation rule rejected the row |
| `INVALID_CROSS_REFERENCE_KEY` | A lookup value points to a record that does not exist |
| `DUPLICATE_VALUE` | A unique field already holds that value |
| `STRING_TOO_LONG` | A value exceeds the field length |
Fix the rows, save only the failed ones to a new file, and upsert that file. This works because upsert ignores records that already loaded correctly.
Limits and sizing
Bulk API 2.0 has daily limits on batches and records, and these depend on the org edition. Scratch orgs have the limits of the edition defined in project-scratch-def.json. Developer edition limits are lower than those of a production org.
- Keep seed files below the size accepted by one job, which is 150 MB per upload. Seed sets rarely come close.
- Split very large loads into several files and run them in sequence.
- Disable or bypass heavy automation when loading volume. Triggers and flows run once per record batch and can cause CPU limit errors.
A practical approach is a custom setting or custom permission that your triggers check. Seed loads then skip nonessential logic. Remember to turn that behavior back on, and never leave it as the default.
Choosing between the approaches
Summary of the workflow
- Deploy the metadata, including the external ID fields.
- Write one CSV per object, with an external ID column on each.
- Load parents first, then children using
Relationship.ExternalIdFieldheaders. - Use
--waitand check the failed count. - Inspect results for failures, correct the rows, and re-run the upsert.
Exercise
A Contact upsert reports that several rows failed with INVALIDCROSSREFERENCEKEY. The Contact CSV has the header Account.ExternalId__c. What are the two most likely causes, and how do you fix them?
Show answer
Either the Accounts file was not loaded before the Contacts, or the Account.ExternalIdc values in the Contact rows do not match any ExternalId__c value on existing Accounts, for example because of a typo or different formatting. Load the Accounts first, confirm the values match exactly, then upsert only the failed Contact rows again.
Exercise
Why is sf data upsert bulk with an external ID safer for a repeated seed script than sf data import bulk?
Show answer
Import bulk always inserts, so each run creates duplicate records. Upsert matches each row on the external ID field, so it updates a record that already exists and inserts only new ones. The result is the same data after every run.