Edits history of script submission #23414 for ' Create Association (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 Association
     * Associate two CRM records. Leave `association_types` empty for the default (unlabeled) association, or pass labels from List Association Labels, e.g. [{"associationCategory": "USER_DEFINED", "associationTypeId": 36}]. Object types other than contacts, companies and deals need their own scope on the connection (e.g. tickets, crm.objects.line_items.read, crm.objects.custom.read).
     */
    export async function main(
      auth: RT.Hubspot,
      from_object_type: DynSelect_object_type,
      from_object_id: string,
      to_object_type: DynSelect_object_type,
      to_object_id: string,
      association_types:
        | {
            associationCategory:
              | "HUBSPOT_DEFINED"
              | "USER_DEFINED"
              | "INTEGRATOR_DEFINED"
            associationTypeId: number
          }[]
        | undefined
    ) {
      const labeled = association_types?.length
      const response = await fetch(
        `https://api.hubapi.com/crm/v4/objects/${from_object_type}/${from_object_id}/associations/${labeled ? "" : "default/"}${to_object_type}/${to_object_id}`,
        labeled
          ? {
              method: "PUT",
              headers: {
                Authorization: `Bearer ${auth.token}`,
                "Content-Type": "application/json",
                Accept: "application/json",
              },
              body: JSON.stringify(association_types),
            }
          : {
              method: "PUT",
              headers: {
                Authorization: `Bearer ${auth.token}`,
                Accept: "application/json",
              },
            }
      )
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 5 hours ago