0

Update Ticket

by
Published Nov 7, 2023

Update a ticket and optionally add a comment to it (public reply, or an internal note when `internal_note` is on). Only the fields you fill are changed. Attach files by passing the tokens returned by Upload Ticket Attachments (needs a comment). Tags are handled by the Add/Set/Remove Ticket Tags actions; set any other field through `fields`.

Script zendesk Verified

The script

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

3
/**
4
 * Update Ticket
5
 * Update a ticket and optionally add a comment to it (public reply, or an internal note when `internal_note` is on). Only the fields you fill are changed. Attach files by passing the tokens returned by Upload Ticket Attachments (needs a comment). Tags are handled by the Add/Set/Remove Ticket Tags actions; set any other field through `fields`.
6
 */
7
export async function main(
8
  auth: RT.Zendesk,
9
  ticket_id: number,
10
  comment: string | undefined,
11
  comment_is_html: boolean,
12
  internal_note: boolean,
13
  upload_tokens: string[] | undefined,
14
  subject: string | undefined,
15
  status: "new" | "open" | "pending" | "hold" | "solved" | "closed" | undefined,
16
  priority: "urgent" | "high" | "normal" | "low" | undefined,
17
  type: "problem" | "incident" | "question" | "task" | undefined,
18
  assignee_id: number | undefined,
19
  assignee_email: string | undefined,
20
  group_id: number | undefined,
21
  custom_fields: { id: number; value: any }[] | undefined,
22
  fields: { [key: string]: any } | undefined
23
) {
24
  const ticket: { [key: string]: any } = {
25
    ...fields,
26
    subject: subject || undefined,
27
    status: status || undefined,
28
    priority: priority || undefined,
29
    type: type || undefined,
30
    assignee_id: assignee_id ?? undefined,
31
    assignee_email: assignee_email || undefined,
32
    group_id: group_id ?? undefined,
33
    custom_fields: custom_fields?.length ? custom_fields : undefined,
34
  }
35
  if (comment) {
36
    ticket.comment = {
37
      ...(comment_is_html ? { html_body: comment } : { body: comment }),
38
      public: !internal_note,
39
      uploads: upload_tokens?.length ? upload_tokens : undefined,
40
    }
41
  }
42

43
  const response = await fetch(
44
    `https://${auth.subdomain}.zendesk.com/api/v2/tickets/${ticket_id}`,
45
    {
46
      method: "PUT",
47
      headers: {
48
        Authorization: auth.token
49
          ? `Bearer ${auth.token}`
50
          : `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
51
        "Content-Type": "application/json",
52
        Accept: "application/json",
53
      },
54
      body: JSON.stringify({ ticket }),
55
    }
56
  )
57

58
  if (!response.ok) {
59
    throw new Error(`${response.status} ${await response.text()}`)
60
  }
61

62
  return await response.json()
63
}
64

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 539 days ago
    1
    type Zendesk = {
    2
      username: string;
    3
      password: string;
    4
      subdomain: string;
    5
    };
    6
    /**
    7
     * Update Ticket
    8
     * Update Ticket
    9
     */
    10
    export async function main(
    11
      auth: Zendesk,
    12
      ticket_id: string,
    13
      body: {
    14
        ticket?: {
    15
          additional_collaborators?: {
    16
            email?: string;
    17
            name?: string;
    18
            [k: string]: unknown;
    19
          }[];
    20
          assignee_email?: string;
    21
          assignee_id?: string;
    22
          attribute_value_ids?: string[];
    23
          collaborator_ids?: string;
    24
          comment?: {
    25
            attachments?: {
    26
              content_type?: string;
    27
              content_url?: string;
    28
              deleted?: string;
    29
              file_name?: string;
    30
              height?: string;
    31
              id?: string;
    32
              inline?: string;
    33
              malware_access_override?: string;
    34
              malware_scan_result?: string;
    35
              mapped_content_url?: string;
    36
              size?: string;
    37
              thumbnails?: {
    38
                content_type?: string;
    39
                content_url?: string;
    40
                deleted?: string;
    41
                file_name?: string;
    42
                height?: string;
    43
                id?: string;
    44
                inline?: string;
    45
                malware_access_override?: string;
    46
                malware_scan_result?: string;
    47
                mapped_content_url?: string;
    48
                size?: string;
    49
                url?: string;
    50
                width?: string;
    51
                [k: string]: unknown;
    52
              }[];
    53
              url?: string;
    54
              width?: string;
    55
              [k: string]: unknown;
    56
            }[];
    57
            audit_id?: string;
    58
            author_id?: string;
    59
            body?: string;
    60
            created_at?: string;
    61
            html_body?: string;
    62
            id?: string;
    63
            metadata?: { amet_c?: string; nisi03_?: boolean; [k: string]: unknown };
    64
            plain_body?: string;
    65
            public?: string;
    66
            type?: string;
    67
            uploads?: string[];
    68
            via?: {
    69
              channel?: string;
    70
              source?: { sit2af?: string; [k: string]: unknown };
    71
              [k: string]: unknown;
    72
            };
    73
            [k: string]: unknown;
    74
          };
    75
          custom_fields?: string;
    76
          custom_status_id?: string;
    77
          due_at?: string;
    78
          email_ccs?: {
    79
            action?: string;
    80
            user_email?: string;
    81
            user_id?: string;
    82
            user_name?: string;
    83
            [k: string]: unknown;
    84
          }[];
    85
          external_id?: string;
    86
          followers?: {
    87
            action?: string;
    88
            user_email?: string;
    89
            user_id?: string;
    90
            [k: string]: unknown;
    91
          }[];
    92
          group_id?: string;
    93
          organization_id?: string;
    94
          priority?: string;
    95
          problem_id?: string;
    96
          requester_id?: string;
    97
          safe_update?: string;
    98
          sharing_agreement_ids?: string[];
    99
          status?: string;
    100
          subject?: string;
    101
          tags?: string;
    102
          type?: string;
    103
          updated_stamp?: string;
    104
          [k: string]: unknown;
    105
        };
    106
        [k: string]: unknown;
    107
      }
    108
    ) {
    109
      const url = new URL(
    110
        `https://${auth.subdomain}.zendesk.com/api/v2/tickets/${ticket_id}`
    111
      );
    112
    
    
    113
      const response = await fetch(url, {
    114
        method: "PUT",
    115
        headers: {
    116
          "Content-Type": "application/json",
    117
          Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
    118
        },
    119
        body: JSON.stringify(body),
    120
      });
    121
      if (!response.ok) {
    122
        const text = await response.text();
    123
        throw new Error(`${response.status} ${text}`);
    124
      }
    125
      return await response.json();
    126
    }
    127