Sub topic 7
Closing the Job to Start Processing
This chapter covers the step that turns an uploaded job into a running one: setting the job state to UploadComplete. It assumes you can read JSON and CSV, know HTTP methods, and have an access token and an open ingest job with data uploaded, as covered in the previous chapters.
Why a separate close step exists
In Bulk API 2.0 an ingest job does not start when you upload data. Salesforce holds the data until you tell it the upload is finished. The job stays in the Open state, and the platform cannot know whether more data is coming.
Closing the job is that signal. You send one PATCH request that sets the state to UploadComplete. Salesforce then queues the job, splits the data into batches internally, and begins processing.
The sequence so far looks like this:
The close request
Send a PATCH to the job resource with a small JSON body.
curl -X PATCH "$INSTANCE_URL/services/data/v62.0/jobs/ingest/$JOB_ID" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"state":"UploadComplete"}'The response is the full job description. The state field normally reads UploadComplete at this point, and the numberRecordsProcessed and numberRecordsFailed fields are still 0. Processing is asynchronous, so the call returns as soon as the state change is accepted. It does not wait for records to be written.
Use the same API version in every call for a job. Mixing versions within one job is a common source of confusing behavior.
What changes after you close the job
Closing is a one-way step for the data. After the state becomes UploadComplete:
- You cannot upload more data to that job. A further
PUTto/batchesis rejected. - You cannot reopen the job. If you forgot rows, create a new job.
- The only state change still allowed is
Aborted, which cancels processing.
Treat the upload and the close as a pair. If your script uploads successfully but crashes before the PATCH, the job stays Open and holds nothing useful. Record the job ID as soon as you create it so a later run can either finish the close or abort the job.
Close versus abort
Both actions use the same endpoint and the same style of request. The state value decides what happens.
| Requested state | Meaning | When to use it |
|---|---|---|
| `UploadComplete` | Data is complete, start processing | After all uploads succeeded |
| `Aborted` | Stop the job and discard pending work | Wrong data, wrong object, or a failed upload |
Aborting does not roll back records that were already processed. If a job was InProgress when you aborted it, some records may already be committed. Check the results endpoints to see which ones.
curl -X PATCH "$INSTANCE_URL/services/data/v62.0/jobs/ingest/$JOB_ID" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"state":"Aborted"}'Closing in a script
In Python, check each step before moving to the next. The most important rule is to close the job only after the upload call has returned a success status.
import os
import requests
instance = os.environ["SF_INSTANCE_URL"]
token = os.environ["SF_ACCESS_TOKEN"]
job_id = os.environ["SF_JOB_ID"]
base = f"{instance}/services/data/v62.0/jobs/ingest"
auth = {"Authorization": f"Bearer {token}"}
with open("contacts.csv", "rb") as f:
upload = requests.put(
f"{base}/{job_id}/batches",
headers={**auth, "Content-Type": "text/csv"},
data=f,
)
upload.raise_for_status()
close = requests.patch(
f"{base}/{job_id}",
headers=auth,
json={"state": "UploadComplete"},
)
close.raise_for_status()
print(close.json()["state"])raise_for_status() stops the script on any 4xx or 5xx response. Without it, a failed upload would be followed by a close request, and Salesforce would process an empty or partial job.
Common errors
Most problems at this step come from one of a few causes.
- Closing a job with no data. If you send
UploadCompletewithout uploading anything, the job cannot do useful work. It typically ends in a failed state or reports zero records. Verify the upload response first. - Closing twice. A second
UploadCompleteon a job that has already moved on is rejected or has no effect. Read the job state with aGETbefore retrying a close that appeared to time out. - Wrong state spelling. State values are case-sensitive.
uploadcompleteandUpload Completeare invalid. UseUploadCompleteexactly. - Wrong content type. The close request carries JSON, so send
Content-Type: application/json. Do not reuse thetext/csvheader from the upload. - Expired token. A long upload can outlast an access token. A
401on thePATCHmeans the data is uploaded but the job is stillOpen. Refresh the token and send the close again.
The last two cases show why a retry should start with a read. A GET on the job tells you whether the close took effect.
curl "$INSTANCE_URL/services/data/v62.0/jobs/ingest/$JOB_ID" \
-H "Authorization: Bearer $ACCESS_TOKEN"If state is Open, send the close again. If it is UploadComplete or later, do nothing.
Timing and what happens next
The delay between UploadComplete and InProgress depends on platform load and on the queue of other jobs in your org. It can be a few seconds or considerably longer. Do not treat a slow start as an error.
Once processing begins, Salesforce divides your data into batches and runs them. You do not control the batch size in Bulk API 2.0. The next chapters explain how to watch the job move through its states, then how to read the per-record results.
Summary of the sequence
- Create the job with
POST. The state isOpen. - Upload the CSV with
PUTto/batches. The state remainsOpen. - Close the job with
PATCHand{"state":"UploadComplete"}. - Poll with
GETuntil the state isJobComplete,FailedorAborted.
Exercise
Your script uploaded a CSV and then received a 401 response on the PATCH that sets UploadComplete. What state is the job in, what should you do first, and why should you not simply create a new job and upload again?
Show answer
The job is still Open, because the state change was never accepted. Refresh the access token, then send a GET on the job to confirm the state is Open, and send the PATCH again. The uploaded data is already attached to the job, so creating a new job would duplicate the work and, if the first job were later closed or left behind, could load records twice. Abort the first job if you do decide to start over.
Exercise
After closing a job you notice the CSV was missing 200 rows. Can you upload the missing rows to the same job? What are your options?
Show answer
No. Once the state is UploadComplete the job accepts no more data. Create a new ingest job containing only the 200 missing rows, upload it, and close it. If the first job is still queued and its data is wrong, you can abort it with state Aborted instead, but records already processed are not rolled back.