Create Custom Object Field

Creates any of the following custom field types: * text (default when no "type" is specified) * textarea * checkbox * date * integer * decimal * regexp * dropdown * lookup See [About custom field types](https://support.zendesk.com/hc/en-us/articles/203661866) in Zendesk help. #### Allowed For * Admins

Script zendesk Verified

by hugo697 ยท 11/7/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 377 days ago
1
type Zendesk = {
2
  username: string;
3
  password: string;
4
  subdomain: string;
5
};
6
/**
7
 * Create Custom Object Field
8
 * Creates any of the following custom field types:
9

10
* text (default when no "type" is specified)
11
* textarea
12
* checkbox
13
* date
14
* integer
15
* decimal
16
* regexp
17
* dropdown
18
* lookup
19

20
See [About custom field types](https://support.zendesk.com/hc/en-us/articles/203661866) in Zendesk help.
21

22
#### Allowed For
23

24
* Admins
25

26
 */
27
export async function main(
28
  auth: Zendesk,
29
  custom_object_key: string,
30
  body: {
31
    custom_object_field?: {
32
      active?: string;
33
      created_at?: string;
34
      custom_field_options?: {
35
        id?: string;
36
        name?: string;
37
        position?: string;
38
        raw_name?: string;
39
        url?: string;
40
        value?: string;
41
        [k: string]: unknown;
42
      }[];
43
      description?: string;
44
      id?: string;
45
      key?: string;
46
      position?: string;
47
      raw_description?: string;
48
      raw_title?: string;
49
      regexp_for_validation?: string;
50
      relationship_filter?: { [k: string]: unknown };
51
      relationship_target_type?: string;
52
      system?: string;
53
      tag?: string;
54
      title?: string;
55
      type?: string;
56
      updated_at?: string;
57
      url?: string;
58
      [k: string]: unknown;
59
    };
60
    [k: string]: unknown;
61
  }
62
) {
63
  const url = new URL(
64
    `https://${auth.subdomain}.zendesk.com/api/v2/custom_objects/${custom_object_key}/fields`
65
  );
66

67
  const response = await fetch(url, {
68
    method: "POST",
69
    headers: {
70
      "Content-Type": "application/json",
71
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
72
    },
73
    body: JSON.stringify(body),
74
  });
75
  if (!response.ok) {
76
    const text = await response.text();
77
    throw new Error(`${response.status} ${text}`);
78
  }
79
  return await response.json();
80
}
81