Sub topic 6
Creating a Record with POST
This chapter assumes you can send HTTP requests with curl or Python, read JSON, and already hold a valid OAuth access token for a Salesforce org. It also assumes you know what an sObject, a field, and a record ID are. Here you will create an Account with one POST request and read the ID from the response.
The request
To create a record, send a POST to the sObject resource URL. The URL has three parts: the instance URL, the API version, and the sObject name.
{instance URL}/services/data/{version}/sobjects/{SObjectName}The request needs two headers and a JSON body:
Authorization: Bearer <access token>identifies you.Content-Type: application/jsontells Salesforce how to parse the body.- The body is a JSON object. Each key is a field API name and each value is the data for that field.
export SF_INSTANCE_URL="https://MyDomain.my.salesforce.com"
export SF_ACCESS_TOKEN="<access token>"
export SF_API_VERSION="v60.0"
curl -X POST "$SF_INSTANCE_URL/services/data/$SF_API_VERSION/sobjects/Account" \
-H "Authorization: Bearer $SF_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"Name": "Acme Corp", "Industry": "Technology"}'Do not send an Id in the body. Salesforce generates it.
The response
A successful create returns status 201 Created and a JSON body.
{
"id": "001xx000003DGb2AAG",
"success": true,
"errors": []
}The id value here is a placeholder. Your org returns a real ID. Save it, because every later read or update of this record uses it in the URL. The errors array is empty on success.
The same call in Python
This version uses the requests library. It reads the credentials from environment variables and fails with the response body when the call does not succeed.
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",
}
response = requests.post(
f"{base}/sobjects/Account",
headers=headers,
json={"Name": "Acme Corp", "Industry": "Technology"},
timeout=30,
)
if not response.ok:
# Salesforce returns a JSON array of error objects
raise RuntimeError(f"{response.status_code}: {response.text}")
account_id = response.json()["id"]
print(response.status_code, account_id)Passing json= makes requests serialize the dictionary. Passing json= also sets the body as JSON, but keeping the explicit Content-Type header makes the intent clear and works the same for other clients.
Common errors
A failed create returns a 4xx status and a JSON array of error objects, not a single object. A missing required field looks like this:
[
{
"message": "Required fields are missing: [Name]",
"errorCode": "REQUIRED_FIELD_MISSING",
"fields": ["Name"]
}
]Check these causes first:
| Symptom | Likely cause |
|---|---|
| 401 with `INVALID_SESSION_ID` | The access token is expired or invalid, or the instance URL is wrong. |
| 400 with `REQUIRED_FIELD_MISSING` | The body leaves out a field the object requires. |
| 400 with `INVALID_FIELD` | A field name is misspelled or does not exist on the object. |
| 404 with `NOT_FOUND` | The sObject name or API version in the URL is wrong. |
Exercise
Exercise
You POST {"Industry": "Technology"} to the Account resource and get status 400. What does the body contain, and how do you fix the request?
Show answer
The body is a JSON array with an object whose errorCode is REQUIREDFIELDMISSING and whose fields list includes Name. Add a Name value to the JSON body and send the request again. A successful retry returns 201 with the new record ID.
Exercise
A create call returns 201. Which part of the response do you keep for later reads and updates?
Show answer
The id value in the JSON body. It goes at the end of the record URL, as in /sobjects/Account/<id>.