Create Associations ( hubspot)
One script reply has been approved by the moderators Verified

Created by maximehacecadahut 296 days ago Viewed 52 times 0 Points

No comments yet

Login to be able to comment
Points: 0
deno
One script reply has been approved by the moderators
Ap­pro­ved
import * as wmill from "https://deno.land/x/windmill@v1.70.1/mod.ts"
import { Client } from "npm:@hubspot/api-client@^8.1.0"
import type { AssociationSpec } from "npm:@hubspot/api-client@^8.1.0/lib/codegen/crm/contacts/index.js";

export async function main(
  auth: wmill.Resource<"hubspot">,
  contact_id: number,
  to_object_type: string,
  to_object_id: number,
  associations: AssociationSpec[]
) {
  const client = new Client({
    accessToken: auth.token
  })

  try {
    associations = associations?.map(c => {
      return typeof c === 'string' ? JSON.parse(c) : c
    }) || []
  } catch(error) {
    throw Error(`Tried to parse "associations" argument because 
    it was an array of strings but failed with error:\n${error}
    Associations must have the following shape:
      {
        associationCategory: 'HUBSPOT_DEFINED' | 'USER_DEFINED' | 'INTEGRATOR_DEFINED',
        associationTypeId: number
      }
    `)
  }

  try {
    return await client.crm.contacts.associationsApi.create(
      contact_id,
      to_object_type,
      to_object_id,
      associations
    )
  } catch (e) {
    throw Error(`
      ${e.code}\n
      Message: ${e.body.message || e.body}\n
    `)
  }
}

Submitted by adam186 114 days ago

Edited 29 days ago

No comments yet

Login to be able to comment