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 | * Create Record |
30 | * Create a record of any object (Lead, Contact, Account, Opportunity, Case, Task, or a custom object). Pass the object's API name and a field map. |
31 | */ |
32 | export async function main( |
33 | auth: RT.Salesforce, |
34 | sobject: DynSelect_sobject, |
35 | body: { [key: string]: any } |
36 | ) { |
37 | const apiVersion = auth.api_version || "v60.0" |
38 | const url = new URL( |
39 | `${auth.instance_url}/services/data/${apiVersion}/sobjects/${sobject}` |
40 | ) |
41 |
|
42 | const response = await fetch(url, { |
43 | method: "POST", |
44 | headers: { |
45 | Authorization: `Bearer ${auth.token}`, |
46 | "Content-Type": "application/json", |
47 | Accept: "application/json", |
48 | }, |
49 | body: JSON.stringify(body), |
50 | }) |
51 |
|
52 | if (!response.ok) { |
53 | throw new Error(`${response.status} ${await response.text()}`) |
54 | } |
55 |
|
56 | return await response.json() |
57 | } |
58 |
|