Sub topic 11
Discovering fields and required values with describe
This chapter assumes you can already send authenticated requests with curl or Python, and that you have the SF_INSTANCE_URL, SF_ACCESS_TOKEN, and SF_API_VERSION variables from earlier chapters. You should also know what a create error looks like, such as REQUIRED_FIELD_MISSING.
Why describe exists
Guessing field names causes most create and update failures. A field's API name can differ from its label. A custom field ends in __c. A field can also exist but not be writable by your user.
The describe resource returns the object's metadata, so your code can check these things instead of guessing. It is a GET on the sObject URL with /describe added:
curl "$SF_INSTANCE_URL/services/data/$SF_API_VERSION/sobjects/Account/describe" \
-H "Authorization: Bearer $SF_ACCESS_TOKEN"The response is large. It lists object-level properties such as createable and updateable, and a fields array with one entry per field.
Reading a field entry
Each entry in fields has many properties. These are the ones you will use most:
| Property | Meaning |
|---|---|
| `name` | The API name to use in request bodies |
| `label` | The name shown in the UI |
| `type` | Data type, such as `string`, `picklist`, `reference`, or `boolean` |
| `nillable` | `true` if the field may be left empty |
| `createable` | `true` if you can set it with POST |
| `updateable` | `true` if you can change it with PATCH |
| `defaultedOnCreate` | `true` if Salesforce fills in a value when you omit it |
| `picklistValues` | Allowed values for picklist fields |
| `referenceTo` | Object types a lookup field can point to |
Finding required fields
A field must be supplied on create when all three conditions hold: createable is true, nillable is false, and defaultedOnCreate is false. Fields such as Id and CreatedDate are not required, because you cannot set them.
The same check works in a script:
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")
url = f"{instance_url}/services/data/{version}/sobjects/Account/describe"
response = requests.get(
url,
headers={"Authorization": f"Bearer {token}"},
timeout=30,
)
if not response.ok:
raise RuntimeError(f"{response.status_code}: {response.text}")
fields = response.json()["fields"]
required = [
f["name"]
for f in fields
if f["createable"] and not f["nillable"] and not f["defaultedOnCreate"]
]
print("Required on create:", required)
for f in fields:
if f["name"] == "Industry":
values = [p["value"] for p in f["picklistValues"] if p["active"]]
print("Industry values:", values)For a standard Account, the required list is usually just Name. Your org may add more through custom fields or validation settings.
Checking picklist values
A picklist field rejects values outside its list. The second half of the script reads picklistValues and keeps only entries where active is true. Compare against value, not label. The two can differ, and the API expects value.
Describe responses are metadata, not rules
Describe reflects what the current user can do, but it does not cover everything.
- Validation rules and required-by-page-layout settings do not appear as
nillable: false. A create can still fail with aFIELD_CUSTOM_VALIDATION_EXCEPTIONerror. - Field-level security changes the result. Another user may see a different
fieldsarray. - The response is large, so cache it per object and API version rather than calling it before every request.
Exercise
A describe entry for the field Region__c has createable: true, nillable: false, and defaultedOnCreate: false. Another field, Status__c, has createable: true, nillable: false, and defaultedOnCreate: true. Which one must you include in a POST body, and why?
Show answer
You must include Region__c. It can be set, it cannot be empty, and Salesforce does not supply a value. Status__c is optional because Salesforce fills in a default when you omit it.