Edits history of script submission #23823 for ' Update Ticket (zendesk)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Update Ticket
     * 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`.
     */
    export async function main(
      auth: RT.Zendesk,
      ticket_id: number,
      comment: string | undefined,
      comment_is_html: boolean,
      internal_note: boolean,
      upload_tokens: string[] | undefined,
      subject: string | undefined,
      status: "new" | "open" | "pending" | "hold" | "solved" | "closed" | undefined,
      priority: "urgent" | "high" | "normal" | "low" | undefined,
      type: "problem" | "incident" | "question" | "task" | undefined,
      assignee_id: number | undefined,
      assignee_email: string | undefined,
      group_id: number | undefined,
      custom_fields: { id: number; value: any }[] | undefined,
      fields: { [key: string]: any } | undefined
    ) {
      const ticket: { [key: string]: any } = {
        ...fields,
        subject: subject || undefined,
        status: status || undefined,
        priority: priority || undefined,
        type: type || undefined,
        assignee_id: assignee_id ?? undefined,
        assignee_email: assignee_email || undefined,
        group_id: group_id ?? undefined,
        custom_fields: custom_fields?.length ? custom_fields : undefined,
      }
      if (comment) {
        ticket.comment = {
          ...(comment_is_html ? { html_body: comment } : { body: comment }),
          public: !internal_note,
          uploads: upload_tokens?.length ? upload_tokens : undefined,
        }
      }
    
      const response = await fetch(
        `https://${auth.subdomain}.zendesk.com/api/v2/tickets/${ticket_id}`,
        {
          method: "PUT",
          headers: {
            Authorization: auth.token
              ? `Bearer ${auth.token}`
              : `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
            "Content-Type": "application/json",
            Accept: "application/json",
          },
          body: JSON.stringify({ ticket }),
        }
      )
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 12 days ago