Sub topic 2
Creating, Reading, and Updating Records with the Salesforce REST API
Assume you know basic HTTP methods, headers, status codes, and JSON. You also need a Salesforce org and an OAuth 2.0 access token.
Creating a Record with POST
Salesforce exposes each object as an sObject resource. Build its URL from the instance URL, API version, and object name:
https://INSTANCE/services/data/VERSION/sobjects/OBJECTUse POST to create a record. Send fields as a JSON object and authenticate with a bearer token.
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"}'A successful create returns HTTP 201 Created and the new record ID:
{
"id": "001xx000003DGb2AAG",
"success": true,
"errors": []
}Save this ID for later operations.
Reading a Record with GET
Use GET and the record ID to retrieve one record. The fields query parameter limits the response and avoids returning unnecessary data.
curl "$SF_INSTANCE_URL/services/data/$SF_API_VERSION/sobjects/Account/001xx000003DGb2AAG?fields=Name,Industry" \
-H "Authorization: Bearer $SF_ACCESS_TOKEN"The response contains the requested fields as JSON. A missing or inaccessible ID usually produces an error response instead of a record.
Updating a Record with PATCH
Use PATCH on the record URL to change selected fields. The request body does not need to contain the complete record.
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"}' \
-iA successful update returns 204 No Content with an empty body. A 401 Unauthorized response commonly means that the access token expired. A 400 Bad Request response can identify missing required fields or invalid field values.
Exercise
Which HTTP method creates a record, and which method changes selected fields on an existing record?
Show answer
POST creates a record. PATCH updates selected fields on an existing record.