Sub topic 8
Updating a record with PATCH
This chapter assumes you can already build a resource URL, send a bearer token in the Authorization header, and create and read a record. It uses the same environment variables as the earlier chapters: SF_INSTANCE_URL, SF_ACCESS_TOKEN, and SF_API_VERSION.
How PATCH updates a record
You update a record by sending a PATCH request to the record's URL, which is the sObject resource URL followed by the record ID. The body is a JSON object that contains only the fields you want to change. Salesforce leaves every field you don't mention as it was.
curl -X PATCH "$SF_INSTANCE_URL/services/data/$SF_API_VERSION/sobjects/Account/001xx000003DGb2AAG" \
-H "Authorization: Bearer $SF_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"Industry": "Banking"}' \
-iThe -i flag makes curl print the response headers. You need them here because a successful update has no body, so the status line is the only confirmation you get.
The 204 No Content response
A successful update returns 204 No Content and an empty body. Don't call .json() on this response, because there is nothing to parse and most JSON parsers raise an error on an empty string. Check the status code instead.
HTTP/1.1 204 No ContentIf you need the new values, send a GET request afterward and use the fields parameter to limit the response.
The request flow
Updating from Python
This script creates an account, reads it, and then updates it. It uses the requests library and raises an error that includes the response body whenever a call fails. The update step is the part that matters for this chapter.
import os
import requests
instance_url = os.environ["SF_INSTANCE_URL"]
token = os.environ["SF_ACCESS_TOKEN"]
version = os.environ.get("SF_API_VERSION", "v60.0")
base = f"{instance_url}/services/data/{version}"
headers = {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json",
}
def check(response):
if not response.ok:
# Salesforce returns a JSON array of {message, errorCode, ...} objects
raise RuntimeError(f"{response.status_code}: {response.text}")
return response
# Create
created = check(requests.post(
f"{base}/sobjects/Account",
headers=headers,
json={"Name": "Acme Corp", "Industry": "Technology"},
timeout=30,
)).json()
account_id = created["id"]
# Read
account = check(requests.get(
f"{base}/sobjects/Account/{account_id}",
headers=headers,
params={"fields": "Name,Industry"},
timeout=30,
)).json()
print(account["Name"], account["Industry"])
# Update
updated = check(requests.patch(
f"{base}/sobjects/Account/{account_id}",
headers=headers,
json={"Industry": "Banking"},
timeout=30,
))
print(updated.status_code) # 204The script never reads a body from the update response. It only prints the status code.
Common errors
Most failed updates come from a few causes. Each one returns a JSON array of error objects, the same shape you saw in the error handling chapter.
| Status | Typical `errorCode` | Cause |
|---|---|---|
| 400 | `INVALID_FIELD` | The body names a field that doesn't exist on the object. |
| 400 | `INVALID_OR_NULL_FOR_RESTRICTED_PICKLIST` | The value isn't in a restricted picklist. |
| 400 | `REQUIRED_FIELD_MISSING` | The update would leave a required field empty. |
| 401 | `INVALID_SESSION_ID` | The access token is expired or invalid. |
| 404 | `NOT_FOUND` | The record ID doesn't exist, or the URL is wrong. |
Exercise
Exercise
Your script sends a PATCH to update an Account's Industry, then calls response.json() and crashes with a JSON decode error, even though the record was updated. What went wrong, and how do you fix it?
Show answer
A successful PATCH returns 204 No Content with an empty body, so there is no JSON to parse. The update worked. Remove the .json() call and check response.status_code == 204 instead. If you need the new values, send a GET request with the fields parameter.
Exercise
You want to set an Account's Phone to a new value and leave every other field alone. What should the request body contain?
Show answer
Only the changed field, for example {"Phone": "555-0100"}. Fields left out of the body keep their current values.