0

updateTicket

by
Published Apr 18, 2023

updates a hubspot ticket with provided properties

Script hubspot
  • Submitted by sindre svendby964 Deno
    Created 1152 days ago
    1
    // Ctrl+. to cache dependencies on imports hover, Ctrl+S to format.
    2
    
    
    3
    import * as wmill from "https://deno.land/x/[email protected]/mod.ts";
    4
    
    
    5
    type HS<TObj> = {
    6
      id: string;
    7
      createdAt: string;
    8
      updatedAt: string;
    9
      archived: boolean;
    10
      properties: {
    11
        createdate: string;
    12
        hs_lastmodifieddate: string;
    13
        hs_object_id: string;
    14
      } & TObj;
    15
    };
    16
    
    
    17
    export async function main<Props extends Record<PropertyKey, string>>(
    18
      hubspot: wmill.Resource<"hubspot">,
    19
      ticketId: number,
    20
      properties: Props,
    21
    ): Promise<HS<Props>> {
    22
      const res = await fetch(
    23
        "https://api.hubapi.com/crm/v3/objects/tickets/" + ticketId,
    24
        {
    25
          headers: {
    26
            Authorization: `Bearer ${hubspot.token}`,
    27
            Accept: "application/json",
    28
            "content-type": "application/json",
    29
          },
    30
          method: "PATCH",
    31
          body: JSON.stringify({
    32
            properties,
    33
          }),
    34
        },
    35
      );
    36
    
    
    37
      if (!res.ok) {
    38
        console.error(res);
    39
        throw new Error(res.statusText);
    40
      }
    41
      const jsonRes = await res.json();
    42
      if (jsonRes.status == "error") {
    43
        console.error(jsonRes);
    44
        throw new Error(jsonRes.message);
    45
      }
    46
      return jsonRes;
    47
    }
    48