How to set multiple associations between 2 entities?

I need to set multiple associations between a Contact and a Company via a workflow using the API

I have a contact that I need to mark as PrimaryContact and BillingContact. This works fine via the Hubspot UI, but when I try to set an additional association via the API it overwrites the first association

The example below results in 1 association against the company

const fromObjectType = "companies";const fromObjectId = companyToAssociate;const toObjectType = "contact";const toObjectId = contactIdToAssociate;

const AssociationSpec = [ { "associationCategory": "USER_DEFINED", "associationTypeId": 124, }];const createContactAssociationResponse = await hubspotClient.apiRequest({ method: 'PUT', path: /crm/v4/objects/${fromObjectType}/${fromObjectId}/associations/${toObjectType}/${toObjectId}, body: AssociationSpec});const AssociationSpec2 = [ { "associationCategory": "USER_DEFINED", "associationTypeId": 113, }];const createContactAssociationResponse2 = await hubspotClient.apiRequest({ method: 'PUT', path: /crm/v4/objects/${fromObjectType}/${fromObjectId}/associations/${toObjectType}/${toObjectId}, body: AssociationSpec2});

Hello @DCozens thank you for posting in our Community!

Associations represent the relationships between objects and activities in the HubSpot CRM. Record associations can exist between records of different objects (e.g., Contact to Company), as well as within the same object (e.g., Company to Company). You can use the associations endpoints to create, retrieve, update, or delete associations between records, or records and activities.

I want to share this developer doc here with more information.

And invite our top experts @Anton, and @Kevin-C any recommendations to @DCozens.

Thank you,

Pam

OK, so I solved this by myself.

When you PUT the associations for an entity and you need multiple labelled associations between the same two entities, you need to send an array of all the associations. This will overwrite any associations previous applied between the two entities

Hello @DCozens, thank you very much for sharing how you solved it with us.

Kindly,

Pam