Sub topic 3
Reading and Handling Error Responses
Assume you know HTTP methods, status codes, JSON, Salesforce objects, and record IDs. You also need an access token and basic curl or Python skills.
Reading Error Responses
Salesforce usually returns an array of error objects. Read both the HTTP status and the errorCode.
| Status | Common meaning | Example error code |
|---|---|---|
| 400 | Invalid request or missing data | `REQUIRED_FIELD_MISSING` |
| 401 | Expired or invalid access token | `INVALID_SESSION_ID` |
| 404 | Resource or record does not exist | `NOT_FOUND` |
[
{
"message": "Required fields are missing: [Name]",
"errorCode": "REQUIRED_FIELD_MISSING",
"fields": ["Name"]
}
]Your client should log the status and response body, refresh authentication for a 401 when appropriate, and fix request data for a 400. Do not retry every error automatically.
Finding Records with SOQL
SOQL, or Salesforce Object Query Language, finds records through the /query resource. Pass the query in the q parameter and select only the fields you need.
curl -G "$SF_INSTANCE_URL/services/data/$SF_API_VERSION/query" \
-H "Authorization: Bearer $SF_ACCESS_TOKEN" \
--data-urlencode "q=SELECT Id, Name FROM Account WHERE Industry = 'Banking' LIMIT 10"The response contains matching records under records. Use nextRecordsUrl when Salesforce indicates that more results remain.
Discovering Fields with Describe
The describe resource reports an object's fields, types, permissions, and metadata. Use it before writing a generic integration or when you need to discover required fields:
GET /services/data/v60.0/sobjects/Account/describeA field whose metadata says createable: true and nillable: false may be required. Confirm the object’s rules and validation requirements in your org.
Upserting by External ID
An upsert creates or updates a record using an External ID field instead of a Salesforce record ID:
PATCH /services/data/v60.0/sobjects/Account/External_Id__c/ACME-100Use this when another system owns the stable identifier. Salesforce updates a matching record or creates one if no match exists.
Composite sObjects
Composite sObject endpoints combine multiple record operations in one request. Use them to reduce network round trips while keeping each record’s result identifiable. They are useful for related creates and moderate batches, but they are not unlimited bulk processing.
Limits and Practical Practices
- Use the API limit information and monitor consumption.
- Request only required fields.
- Batch compatible work with Composite sObjects.
- Retry transient failures with bounded exponential backoff.
- Never place access tokens in source control or logs.
Exercise
Which response details should a client preserve when a create request fails because Name is missing?
Show answer
Preserve the HTTP status, the message, REQUIRED_FIELD_MISSING, and the fields value containing Name.