0

Set Custom Ticket Fields

by
Published today

Set one or more custom field values on a ticket, e.g. `[{ "id": 360001234, "value": "gold" }]`. Field ids come from List Ticket Fields; dropdown fields take the option's `value` tag, multi-select fields an array of tags, date fields `YYYY-MM-DD`.

Script zendesk Verified

The script

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

3
/**
4
 * Set Custom Ticket Fields
5
 * Set one or more custom field values on a ticket, e.g. `[{ "id": 360001234, "value": "gold" }]`. Field ids come from List Ticket Fields; dropdown fields take the option's `value` tag, multi-select fields an array of tags, date fields `YYYY-MM-DD`.
6
 */
7
export async function main(
8
  auth: RT.Zendesk,
9
  ticket_id: number,
10
  custom_fields: { id: number; value: any }[]
11
) {
12
  const response = await fetch(
13
    `https://${auth.subdomain}.zendesk.com/api/v2/tickets/${ticket_id}`,
14
    {
15
      method: "PUT",
16
      headers: {
17
        Authorization: auth.token
18
          ? `Bearer ${auth.token}`
19
          : `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
20
        "Content-Type": "application/json",
21
        Accept: "application/json",
22
      },
23
      body: JSON.stringify({ ticket: { custom_fields } }),
24
    }
25
  )
26

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

31
  return await response.json()
32
}
33