Sub topic 4
Preparing your CSV file
This chapter assumes you know Salesforce objects, fields and external ID fields, and can read CSV. Bulk API 2.0 accepts data only as CSV, so most failed loads trace back to a malformed file. You will learn the rules the file must follow and how to check it before you upload.
Header row and field names
The first row names the fields. Each name must be the field's API name, not its label. Custom fields end in __c. Each later row is one record.
FirstName,LastName,Email
Ada,Lovelace,[email protected]
Alan,Turing,[email protected]Column order does not matter, but every row must have the same number of columns as the header. A header that names a field that does not exist on the object causes the job to fail.
Rules for values
- Empty values: An empty cell is ignored on insert and update. The existing value stays. To clear a field, use the literal value
#N/A. - Commas, quotes and line breaks: Wrap the value in double quotes. Write a literal double quote as two double quotes.
- Dates and times: Use ISO 8601, such as
2026-12-01for dates and2026-12-01T09:09:00Zfor date-times. - Relationships: To link a record to its parent, use the parent's ID in the lookup field, such as
AccountId. You can also use an external ID, with a header in the formAccount.External_Id__c. - Encoding: Save the file as UTF-8.
FirstName,LastName,Title,Description
Grace,Hopper,"Rear Admiral, USN","Wrote the first ""compiler"""Match the line ending to the job
Your job request sets lineEnding to LF or CRLF. The file must use the same one. A mismatch can cause Salesforce to read the whole file as a single row. Check your file with file contacts.csv. Output that mentions "CRLF line terminators" means Windows endings.
Upload with --data-binary in curl so the tool does not alter line endings.
Check the file before uploading
A short script catches the common errors early. This one checks that every row has the same column count as the header, and that the file is valid UTF-8.
import csv
import sys
path = sys.argv[1]
with open(path, newline="", encoding="utf-8") as f:
reader = csv.reader(f)
header = next(reader)
print("Columns:", header)
bad = 0
for number, row in enumerate(reader, start=2):
if len(row) != len(header):
bad += 1
print(f"Row {number}: expected {len(header)} columns, found {len(row)}")
print("Problem rows:", bad)Run it with python check_csv.py contacts.csv. A file that opens without a decoding error and reports zero problem rows has a sound structure. It does not prove the values are valid. Field validation rules and required fields are checked by Salesforce, and violations appear in the failed results.
Size and structure limits
Keep one upload under 150 MB after base64 encoding. Because base64 adds about a third, plan on roughly 100 MB of raw CSV per job. Split larger files into several jobs.
Exercise
Your job request sets lineEnding to LF, but the CSV was saved on Windows. What is the likely result, and how do you check the file's line endings?
Show answer
Salesforce may fail to split the file into rows correctly, so the job can fail or read the data as one row. Run file contacts.csv and look for "CRLF line terminators". Then either convert the file to LF or create the job with "lineEnding":"CRLF".
Exercise
How do you clear the Title field on existing Contact records with an update job?
Show answer
Put the literal value #N/A in the Title column for those rows. An empty cell leaves the existing value unchanged.