Sub topic 7
Reading a Record with GET
This chapter covers retrieving a single record by ID with a GET request and limiting the response to the fields you need. It assumes you can already build a resource URL, send a bearer token, and create a record with POST, as covered in earlier chapters. It also assumes basic comfort with curl or Python and with JSON.
The request
To read a record, send a GET request to the sObject resource URL with the record ID at the end:
/services/data/{version}/sobjects/{SObjectName}/{recordId}A GET has no request body, so you don't need a Content-Type header. You still need the Authorization header.
The ID comes from the id field of the create response. Use the ID of the Account you created in the previous chapter.
curl "$SF_INSTANCE_URL/services/data/$SF_API_VERSION/sobjects/Account/001xx000003DGb2AAG" \
-H "Authorization: Bearer $SF_ACCESS_TOKEN"A successful read returns status 200 OK. The body is a JSON object that contains every field your user can see. It also has an attributes object with the object type and the record's own URL. Without a field list, the response is large because it holds all of the record's fields.
Limiting fields with the fields parameter
Add the fields query parameter with a comma-separated list of API names. The response then holds only those fields, plus attributes and Id.
curl "$SF_INSTANCE_URL/services/data/$SF_API_VERSION/sobjects/Account/001xx000003DGb2AAG?fields=Name,Industry" \
-H "Authorization: Bearer $SF_ACCESS_TOKEN"The response looks like this:
{
"attributes": {
"type": "Account",
"url": "/services/data/v60.0/sobjects/Account/001xx000003DGb2AAG"
},
"Id": "001xx000003DGb2AAG",
"Name": "Acme Corp",
"Industry": "Technology"
}Requesting fewer fields reduces the payload and speeds up the call. Make a habit of always listing the fields you need.
What happens in a read
Reading in Python
The same read with the requests library passes the field list through params, so the library handles encoding:
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")
account_id = "001xx000003DGb2AAG"
response = requests.get(
f"{instance_url}/services/data/{version}/sobjects/Account/{account_id}",
headers={"Authorization": f"Bearer {token}"},
params={"fields": "Name,Industry"},
timeout=30,
)
if not response.ok:
raise RuntimeError(f"{response.status_code}: {response.text}")
account = response.json()
print(account["Name"], account["Industry"])Common errors
| Status | Typical cause | What to check |
|---|---|---|
| 401 | Expired or invalid access token | Get a new token and retry |
| 404 | The ID does not exist, or the URL is wrong | Check the ID, the object name, and the version |
| 400 | A field in `fields` does not exist | Check the spelling of the API name |
| 403 | The user cannot access the record or object | Check profile and sharing settings |
A misspelled field name fails the whole request. Salesforce does not skip the unknown field. A record that exists but is hidden from your user can also appear as a 404, so a 404 does not always mean the record is gone.
Exercise
You call GET on an Account URL with ?fields=Name,Industri. What do you expect, and how do you fix it?
Show answer
The request fails with a 400 error because Industri is not a valid field. The response body is an error array whose message names the bad field. Fix the spelling to Industry and send the request again.
Exercise
Why should you send a fields parameter on most reads?
Show answer
It limits the response to the fields you need. The payload is smaller and the call is faster, and your code does not depend on fields you never use.