Create Custom Object Record
One script reply has been approved by the moderators Verified

Creates a custom object record according to all the properties described by a custom object definition

Allowed For

  • Agents
Created by hugo697 844 days ago
Submitted by hugo697 Typescript (fetch-only)
Verified 298 days ago
1
type Zendesk = {
2
  username: string;
3
  password: string;
4
  subdomain: string;
5
};
6
/**
7
 * Create Custom Object Record
8
 * Creates a custom object record according to all the properties described by a custom object definition
9
#### Allowed For
10
* Agents
11

12
 */
13
export async function main(
14
  auth: Zendesk,
15
  custom_object_key: string,
16
  body: {
17
    custom_object_record?: {
18
      created_at?: string;
19
      created_by_user_id?: string;
20
      custom_object_fields?: { ad_a?: string; [k: string]: unknown };
21
      custom_object_key?: string;
22
      external_id?: string;
23
      id?: string;
24
      name?: string;
25
      updated_at?: string;
26
      updated_by_user_id?: string;
27
      url?: string;
28
      [k: string]: unknown;
29
    };
30
    [k: string]: unknown;
31
  }
32
) {
33
  const url = new URL(
34
    `https://${auth.subdomain}.zendesk.com/api/v2/custom_objects/${custom_object_key}/records`
35
  );
36

37
  const response = await fetch(url, {
38
    method: "POST",
39
    headers: {
40
      "Content-Type": "application/json",
41
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
42
    },
43
    body: JSON.stringify(body),
44
  });
45
  if (!response.ok) {
46
    const text = await response.text();
47
    throw new Error(`${response.status} ${text}`);
48
  }
49
  return await response.json();
50
}
51