0

Describe Object

by
Published 8 days ago

Retrieve metadata for an object — fields, types, picklist values, relationships, and requiredness. Use this to discover an org's picklist values (which are not fixed).

Script salesforce Verified

The script

Submitted by hugo989 Bun
Verified 9 days ago
1
//native
2

3
export type DynSelect_sobject = string
4

5
// Dropdown of the org's objects (global describe), so users pick instead of typing an API name.
6
export async function sobject(auth: RT.Salesforce) {
7
  const apiVersion = auth.api_version || "v60.0"
8
  const response = await fetch(
9
    `${auth.instance_url}/services/data/${apiVersion}/sobjects/`,
10
    {
11
      headers: {
12
        Authorization: `Bearer ${auth.token}`,
13
        Accept: "application/json",
14
      },
15
    }
16
  )
17
  if (!response.ok) {
18
    throw new Error(`${response.status} ${await response.text()}`)
19
  }
20
  const { sobjects } = (await response.json()) as {
21
    sobjects: { name: string; label: string }[]
22
  }
23
  return sobjects
24
    .map((o) => ({ value: o.name, label: `${o.label} (${o.name})` }))
25
    .sort((a, b) => a.label.localeCompare(b.label))
26
}
27

28
/**
29
 * Describe Object
30
 * Retrieve metadata for an object — fields, types, picklist values, relationships, and requiredness. Use this to discover an org's picklist values (which are not fixed).
31
 */
32
export async function main(auth: RT.Salesforce, sobject: DynSelect_sobject) {
33
  const apiVersion = auth.api_version || "v60.0"
34
  const url = new URL(
35
    `${auth.instance_url}/services/data/${apiVersion}/sobjects/${sobject}/describe`
36
  )
37

38
  const response = await fetch(url, {
39
    method: "GET",
40
    headers: {
41
      Authorization: `Bearer ${auth.token}`,
42
      Accept: "application/json",
43
    },
44
  })
45

46
  if (!response.ok) {
47
    throw new Error(`${response.status} ${await response.text()}`)
48
  }
49

50
  return await response.json()
51
}
52