Sub topic 4
Objects, fields, and record IDs
This chapter covers the Salesforce data model as the REST API exposes it. Later chapters build create, read, and update requests on it. I assume you know basic HTTP and JSON and have seen Salesforce objects and fields in the UI. You also need an org and an access token for the Python example, which sends one request.
How the REST API sees your data
Salesforce stores data in objects. The REST API calls an object an sObject. An object works like a database table, a record works like a row, and a field works like a column. Every request in this tutorial names an sObject in its URL, sends or receives field names and values in JSON, and identifies a record by its ID.
Three rules matter more than the rest:
- The API uses API names, never UI labels. The label "Annual Revenue" is
AnnualRevenuein JSON. - A record is identified by an ID, a string that Salesforce generates.
- A field can be readable, creatable, or updatable independently of the others.
Standard and custom objects
Salesforce ships with standard objects such as Account, Contact, Opportunity, Lead, and Case. Administrators and developers add custom objects. Their API names end in __c, for example Invoice__c.
Custom fields follow the same rule, whether they sit on a standard or a custom object. Account.Region__c is a custom field on a standard object.
| Kind | Example API name | Notes |
|---|---|---|
| Standard object | `Account` | No suffix |
| Custom object | `Invoice__c` | Suffix `__c` |
| Standard field | `Industry` | No suffix |
| Custom field | `Region__c` | Suffix `__c` |
| Relationship name (custom) | `Owner_Account__r` | Suffix `__r`, used when traversing relationships |
If your org uses a namespace, API names carry a prefix, such as acme__Invoice__c. API names are not case sensitive in requests, but use the exact casing from describe output so your code matches the org.
Fields and their JSON types
Every field has a type, and the type decides what JSON value the API accepts and returns.
| Field type | JSON value | Example |
|---|---|---|
| Text, Text Area, Picklist, Email, Phone, URL | string | `"Technology"` |
| Checkbox (boolean) | `true` or `false` | `true` |
| Number, Currency, Percent | number | `1250000.50` |
| Date | string, `YYYY-MM-DD` | `"2026-12-01"` |
| Date/Time | string, ISO 8601 | `"2026-12-01T08:16:15.000+0000"` |
| Lookup or Master-Detail (reference) | string holding a record ID | `"001xx000003DGb2AAG"` |
Some details cause most of the type errors:
- Number values are JSON numbers. Sending
"1250000"as a string often works for numeric fields, but sending"abc"fails withINVALID_TYPE_ON_FIELD_IN_RECORD. - Picklist values must match an active value. A restricted picklist rejects anything else with
INVALID_OR_NULL_FOR_RESTRICTED_PICKLIST. An unrestricted picklist accepts new text. - Clearing a field means sending
null.{"Industry": null}empties the field. Leaving a field out of a PATCH body leaves it unchanged. - Text fields have length limits.
Account.Nameallows 255 characters. Longer values fail withSTRING_TOO_LONG.
Reference fields connect records
A lookup or master-detail field stores the ID of another record. On Contact, the field AccountId points to an Account. To attach a new contact to an account, put the account ID in the body:
{
"LastName": "Rivera",
"AccountId": "001xx000003DGb2AAG"
}If the ID does not exist, or points to the wrong kind of object, the call fails with INVALID_CROSS_REFERENCE_KEY. A reference field can only hold an ID of the object type it targets, so a Contact's AccountId cannot hold an Opportunity ID.
Read-only and system fields
Not every field accepts input. These system fields are set by Salesforce:
IdCreatedDateandCreatedByIdLastModifiedDateandLastModifiedByIdSystemModstampIsDeleted
Sending one of them on a create or update usually fails. The common error is INVALID_FIELD_FOR_INSERT_UPDATE, or a message that the field is not writeable. Formula fields and auto-number fields are also read-only.
A field also has to be visible to the integration user. Field-level security hides fields that the user's profile or permission sets do not grant. A hidden field behaves as if it did not exist. Reading it returns nothing, and writing it returns INVALID_FIELD. A missing object permission can make the whole object return NOT_FOUND or INSUFFICIENT_ACCESS. If a field name looks right but fails, check the permissions of the user behind your access token.
Record IDs
Every record has an Id that is unique across the org. The REST API returns it as an 18-character string. A record ID has two parts:
- The first three characters are the key prefix, which identifies the object type.
- The remaining characters identify the record. In the 18-character form, the last three are a checksum.
| Prefix | Object |
|---|---|
| `001` | Account |
| `003` | Contact |
| `005` | User |
| `006` | Opportunity |
| `00Q` | Lead |
| `500` | Case |
The prefixes in the table are standard. Custom objects get prefixes that differ between orgs, typically starting with a0 or similar, so do not hard-code them. Use the keyPrefix value from describe instead.
15-character and 18-character IDs
The 15-character form is case sensitive, so 001xx000003DGb2 and 001xx000003dgb2 are different records. Many tools, such as spreadsheets, treat text as case insensitive, which can merge two different IDs into one. The 18-character form adds a three-character suffix that encodes which of the first 15 characters were uppercase. Compared as strings, 18-character IDs stay unique even in case-insensitive tools.
The API accepts both forms in URLs and bodies, and always returns 18 characters. Store and compare the 18-character form. If you receive a 15-character ID from a report or a user, convert it first:
import string
_SUFFIX_CHARS = string.ascii_uppercase + "012345"
def to_18_char_id(record_id: str) -> str:
"""Convert a 15-character Salesforce ID to its 18-character form."""
if len(record_id) == 18:
return record_id
if len(record_id) != 15:
raise ValueError(f"Not a Salesforce ID: {record_id!r}")
suffix = ""
for start in (0, 5, 10):
chunk = record_id[start:start + 5]
bits = sum(1 << i for i, ch in enumerate(chunk) if ch.isupper())
suffix += _SUFFIX_CHARS[bits]
return record_id + suffix
print(to_18_char_id("001xx000003DGb2")) # 001xx000003DGb2AAGEach group of five characters becomes one suffix character. Bit i is set when character i of the group is an uppercase letter. The resulting number from 0 to 31 indexes into A-Z followed by 0-5.
Reading the shape of a record
Every record that the API returns carries an attributes object next to your fields:
{
"attributes": {
"type": "Account",
"url": "/services/data/v60.0/sobjects/Account/001xx000003DGb2AAG"
},
"Id": "001xx000003DGb2AAG",
"Name": "Acme Corp",
"Industry": "Technology"
}attributes.type is the sObject name, and attributes.url is the record's resource path. Do not send attributes back in an update body, because it is not a field. Composite requests are different. They use attributes.type to say which object each record belongs to, and the later chapter on Composite sObjects covers that case.
Finding what exists: a first look at describe
You do not need to guess field names. The describe resource lists every field of an object, with its type and permissions. The next chapters cover it in detail. This short script lists the required fields for creating an Account:
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")
response = requests.get(
f"{instance_url}/services/data/{version}/sobjects/Account/describe",
headers={"Authorization": f"Bearer {token}"},
timeout=30,
)
response.raise_for_status()
describe = response.json()
print("Key prefix:", describe["keyPrefix"])
for field in describe["fields"]:
required = (
field["createable"]
and not field["nillable"]
and not field["defaultedOnCreate"]
)
if required:
print("Required on create:", field["name"], field["type"])A field is required on create when you can write it (createable), it cannot be empty (nillable is false), and Salesforce does not fill in a default (defaultedOnCreate is false). For a standard Account in most orgs, this prints Name. Validation rules and page-layout requirements are not part of describe. They show up only as errors when you save.
Common mistakes
- Using the label instead of the API name.
Annual Revenuefails withINVALID_FIELD. UseAnnualRevenue. - Forgetting
__c.RegionandRegion__care different names. Only the second is the custom field. - Comparing IDs from different sources. Normalize both to 18 characters before comparing.
- Sending system fields. Remove
Id,CreatedDate, andattributesfrom bodies you build out of a previous GET. - Testing with an admin and deploying with a restricted user. Fields you can see in testing can be missing for the integration user.
Exercise
A Contact create request returns INVALID_CROSS_REFERENCE_KEY for the body {"LastName": "Rivera", "AccountId": "006xx000001ABcdAAA"}. What is the most likely cause?
Show answer
The ID starts with the prefix 006, which belongs to Opportunity records. Contact.AccountId is a reference to an Account, so it only accepts an Account ID, which starts with 001.
Exercise
You export record IDs from a report as 15-character strings and want to match them against IDs returned by the REST API. What do you do, and why?
Show answer
Convert the 15-character IDs to the 18-character form, for example with to_18_char_id, and compare those. The API always returns 18 characters. The 15-character form is case sensitive, so comparing it in a case-insensitive tool can match the wrong records.
Exercise
A describe call shows that Account.CreatedDate has createable: false. What happens if you include CreatedDate in a POST body, and what should you do?
Show answer
The request fails, typically with INVALID_FIELD_FOR_INSERT_UPDATE, because the field is read-only. Remove it from the body. Salesforce sets CreatedDate automatically.