Edits history of script submission #23421 for ' Create Meeting (hubspot)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    export type DynSelect_object_type = string
    
    // Dropdown of the standard CRM object types plus the account's custom objects.
    export async function object_type(auth: RT.Hubspot) {
      const standard = [
        "contacts",
        "companies",
        "deals",
        "tickets",
        "line_items",
        "products",
        "quotes",
        "leads",
        "notes",
        "tasks",
        "meetings",
        "calls",
        "emails",
        "communications",
      ].map((o) => ({ value: o, label: o }))
      const response = await fetch("https://api.hubapi.com/crm/v3/schemas", {
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/json",
        },
      })
      // A token without crm.schemas.custom.read still gets the standard objects.
      if (!response.ok) return standard
      const { results } = (await response.json()) as {
        results: { fullyQualifiedName: string; labels: { plural: string } }[]
      }
      return [
        ...standard,
        ...results.map((s) => ({
          value: s.fullyQualifiedName,
          label: `${s.labels.plural} (custom)`,
        })),
      ]
    }
    
    /**
     * Create Meeting
     * Log a meeting engagement, optionally attached to a contact, company, deal, ticket or other record. Times are ISO 8601. This records the meeting in the CRM; it does not send calendar invites.
     */
    export async function main(
      auth: RT.Hubspot,
      title: string,
      start_time: string,
      to_object_type: DynSelect_object_type | undefined,
      to_object_id: string | undefined,
      end_time: string | undefined,
      meeting_body: string | undefined,
      location: string | undefined,
      outcome:
        | "SCHEDULED"
        | "COMPLETED"
        | "RESCHEDULED"
        | "NO_SHOW"
        | "CANCELED"
        | undefined,
      owner_id: string | undefined
    ) {
      const properties: { [key: string]: any } = {
        hs_meeting_title: title,
        hs_meeting_body: meeting_body,
        hs_timestamp: start_time,
        hs_meeting_start_time: start_time,
        hs_meeting_end_time: end_time,
        hs_meeting_location: location,
        hs_meeting_outcome: outcome,
        hubspot_owner_id: owner_id,
      }
      for (const key of Object.keys(properties)) {
        if (properties[key] == null || properties[key] === "")
          delete properties[key]
      }
    
      const response = await fetch(
        "https://api.hubapi.com/crm/v3/objects/meetings",
        {
          method: "POST",
          headers: {
            Authorization: `Bearer ${auth.token}`,
            "Content-Type": "application/json",
            Accept: "application/json",
          },
          body: JSON.stringify({ properties }),
        }
      )
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      const created = await response.json()
      if (!to_object_type || !to_object_id) return created
    
      const association = await fetch(
        `https://api.hubapi.com/crm/v4/objects/meetings/${created.id}/associations/default/${to_object_type}/${to_object_id}`,
        {
          method: "PUT",
          headers: {
            Authorization: `Bearer ${auth.token}`,
            Accept: "application/json",
          },
        }
      )
      if (!association.ok) {
        throw new Error(`${association.status} ${await association.text()}`)
      }
    
      return { ...created, association: await association.json() }
    }
    

    Submitted by hugo989 5 hours ago