1 | |
2 |
|
3 | export type DynSelect_sobject = string |
4 |
|
5 | |
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 | * Upsert Record |
30 | * Create or update a record matched by an external ID field. Returns 201 (created) or 200 (updated); the external id field must be marked External ID on the object. |
31 | */ |
32 | export async function main( |
33 | auth: RT.Salesforce, |
34 | sobject: DynSelect_sobject, |
35 | external_id_field: string, |
36 | external_id_value: string, |
37 | body: { [key: string]: any } |
38 | ) { |
39 | const apiVersion = auth.api_version || "v60.0" |
40 | const url = new URL( |
41 | `${auth.instance_url}/services/data/${apiVersion}/sobjects/${sobject}/${external_id_field}/${external_id_value}` |
42 | ) |
43 |
|
44 | const response = await fetch(url, { |
45 | method: "PATCH", |
46 | headers: { |
47 | Authorization: `Bearer ${auth.token}`, |
48 | "Content-Type": "application/json", |
49 | Accept: "application/json", |
50 | }, |
51 | body: JSON.stringify(body), |
52 | }) |
53 |
|
54 | if (!response.ok) { |
55 | throw new Error(`${response.status} ${await response.text()}`) |
56 | } |
57 |
|
58 | if (response.status === 204) { |
59 | return { success: true } |
60 | } |
61 | return await response.json() |
62 | } |
63 |
|