0

List Ticket Fields

by
Published Nov 7, 2023

List the account's system and custom ticket fields, with each dropdown's options (`custom_field_options`). Use a field's `id` in Set Custom Ticket Fields. Cursor-paged: pass `meta.after_cursor` as `cursor` while `meta.has_more` is true.

Script zendesk Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 5 days ago
1
//native
2

3
/**
4
 * List Ticket Fields
5
 * List the account's system and custom ticket fields, with each dropdown's options (`custom_field_options`). Use a field's `id` in Set Custom Ticket Fields. Cursor-paged: pass `meta.after_cursor` as `cursor` while `meta.has_more` is true.
6
 */
7
export async function main(
8
  auth: RT.Zendesk,
9
  page_size: number | undefined,
10
  cursor: string | undefined
11
) {
12
  const url = new URL(
13
    `https://${auth.subdomain}.zendesk.com/api/v2/ticket_fields`
14
  )
15
  url.searchParams.append("page[size]", String(page_size || 100))
16
  if (cursor) {
17
    url.searchParams.append("page[after]", cursor)
18
  }
19

20
  const response = await fetch(url, {
21
    headers: {
22
      Authorization: auth.token
23
        ? `Bearer ${auth.token}`
24
        : `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
25
      Accept: "application/json",
26
    },
27
  })
28

29
  if (!response.ok) {
30
    throw new Error(`${response.status} ${await response.text()}`)
31
  }
32

33
  return await response.json()
34
}
35

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 533 days ago
    1
    type Zendesk = {
    2
      username: string;
    3
      password: string;
    4
      subdomain: string;
    5
    };
    6
    /**
    7
     * List Ticket Fields
    8
     * Returns a list of all system and custom ticket fields in your account.
    9
     */
    10
    export async function main(auth: Zendesk, locale: string | undefined) {
    11
      const url = new URL(
    12
        `https://${auth.subdomain}.zendesk.com/api/v2/ticket_fields`
    13
      );
    14
      for (const [k, v] of [["locale", locale]]) {
    15
        if (v !== undefined && v !== "") {
    16
          url.searchParams.append(k, v);
    17
        }
    18
      }
    19
      const response = await fetch(url, {
    20
        method: "GET",
    21
        headers: {
    22
          Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
    23
        },
    24
        body: undefined,
    25
      });
    26
      if (!response.ok) {
    27
        const text = await response.text();
    28
        throw new Error(`${response.status} ${text}`);
    29
      }
    30
      return await response.json();
    31
    }
    32