Sub topic 3
The job model and job states
This chapter assumes you know the Salesforce data model (objects, fields, record IDs), can call REST endpoints with an OAuth access token, and can read JSON and CSV. It covers the job object and the states it moves through. Later chapters build on it.
What a job is
Bulk API 2.0 works through jobs. A job is a server-side record that describes one unit of work: the object, the operation, and the data format. You create the job, attach data to it, and Salesforce processes the data asynchronously. You do not manage batches yourself. Salesforce splits the data into batches internally.
There are two kinds of job:
- Ingest jobs insert, update, upsert, or delete records. They live under
/jobs/ingest. - Query jobs export records with SOQL. They live under
/jobs/query.
Every job has an id. You use that id in every later call.
Ingest job states
An ingest job moves through a fixed set of states. Some changes are made by you, and others are made by Salesforce.
| State | Meaning | Who sets it |
|---|---|---|
| Open | The job exists and accepts data. | Salesforce, on creation |
| UploadComplete | The upload is closed and the job is queued. | You |
| InProgress | Salesforce is processing the data. | Salesforce |
| JobComplete | All records were processed. Some may still have failed. | Salesforce |
| Failed | The job could not be processed. | Salesforce |
| Aborted | You stopped the job. | You |
JobComplete, Failed, and Aborted are terminal states. A job in a terminal state never changes again.
JobComplete does not mean success
JobComplete means Salesforce finished working through the data. It does not mean every record was saved. Each record succeeds or fails on its own. A job with 100,000 rows can end as JobComplete with 300 failed records.
Check the counts in the job info to see what happened:
numberRecordsProcessed: records Salesforce attempted.numberRecordsFailed: records that were rejected.
Failed is different. It means the job as a whole could not run, so many records may be unprocessed. A later chapter shows how to read the successful, failed, and unprocessed result files.
Reading the state
The job info endpoint returns the current state and counts:
curl "$INSTANCE_URL/services/data/v62.0/jobs/ingest/$JOB_ID" \
-H "Authorization: Bearer $ACCESS_TOKEN"The response is JSON. These are the fields to watch first:
{
"id": "750xx0000000001AAA",
"object": "Contact",
"operation": "insert",
"state": "JobComplete",
"numberRecordsProcessed": 2,
"numberRecordsFailed": 0
}The response has more fields than this example shows.
Query job states
Query jobs use a similar set of states, but you do not upload data or close them. Salesforce starts processing when you create the job. The states are UploadComplete, InProgress, JobComplete, Aborted, and Failed. Read results only after JobComplete.
Common errors
- Uploading to a closed job. Data can only be added while the state is
Open. AfterUploadComplete, uploads are rejected. - Forgetting to close the job. A job left in
Opennever starts processing, so it waits until you close it or it expires. - Treating
JobCompleteas all-succeeded. Always checknumberRecordsFailed. - Reading results too early. Poll until the state is terminal before you request result files.
Exercise
A job reports state JobComplete, numberRecordsProcessed 5000 and numberRecordsFailed 120. Did the job fail? What do you do next?
Show answer
No. JobComplete means Salesforce finished processing, and 4,880 records succeeded. The 120 failures are record-level errors. Retrieve the failed results, fix the causes, and resubmit only those rows in a new job.
Exercise
Why does a job that you created and uploaded data to never start processing?
Show answer
It is still in the Open state. Processing starts only after you set the state to UploadComplete.