# Custom code to assign Primary Company

**URL:** <https://community.hubspot.com/t/custom-code-to-assign-primary-company/133505>\
**Category:** Data Hub\
**Created:** [May 28, 2025, 12:20pm UTC](https://community.hubspot.com/t/custom-code-to-assign-primary-company/133505 "2025-05-28T12:20:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![HZaremba](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/hzaremba/32/202631_2.png) [@HZaremba](https://community.hubspot.com/u/HZaremba)\
**Post date:** [May 28, 2025, 12:20pm UTC](https://community.hubspot.com/t/custom-code-to-assign-primary-company/133505/1 "2025-05-28T12:20:40Z")

</div>

Hi, I need to assign a new company as a primary one. I do this in the automation, via a custom code. At first I start with removing the existing association and later set a new primary company. Everything looks fine in a log, but give no result - the new company is still no a Primary one. It would be great to understand what can be the issue.

I have those two steps

await hubspotClient.apiRequest({  
method: ‘POST’,  
path: ‘/crm/v4/associations/contacts/companies/batch/archive’,  
body: companies.map(c =\> ({  
from: { id: contactId },  
to: [{ id: c.toObjectId }]  
}))  
});  
await hubspotClient.apiRequest({  
method: ‘PUT’,  
path: `/crm/v4/objects/contact/${contactId}/associations/company/${bestCompanyId}`,  
body: [  
{ associationCategory: ‘HUBSPOT\_DEFINED’, associationTypeId: 279 }, // default  
{ associationCategory: ‘HUBSPOT\_DEFINED’, associationTypeId: 1 } // primary  
]  
});  
Could you suggest what can be wrong?

---

<div class="post-metadata">

**Author:** ![Jaycee\_Lewis](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jaycee_lewis/32/61352_2.png) [@Jaycee\_Lewis](https://community.hubspot.com/u/Jaycee_Lewis)\
**Post date:** [May 28, 2025, 9:56pm UTC](https://community.hubspot.com/t/custom-code-to-assign-primary-company/133505/2 "2025-05-28T21:56:13Z")

</div>

Hey, @HZaremba 👋 thank for your question. To confirm, you are doing this via a custom coded workflow action, correct? Have you tried breaking these calls up and testing in Postman? — Jaycee

---

<div class="post-metadata">

**Author:** ![Jaycee\_Lewis](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jaycee_lewis/32/61352_2.png) [@Jaycee\_Lewis](https://community.hubspot.com/u/Jaycee_Lewis)\
**Post date:** [May 28, 2025, 10:02pm UTC](https://community.hubspot.com/t/custom-code-to-assign-primary-company/133505/3 "2025-05-28T22:02:22Z")

</div>

Test 1:

Check current associations

`GET https://api.hubapi.com/crm/v4/objects/contact/{contactId}/associations/company`

Test 2:

Test the Archive call

`POST https://api.hubapi.com/crm/v4/associations/contacts/companies/batch/archive`

```lia-code-sample
[{
 "from": { "id": "your-contact-id" },
 "to": { "id": "company-id-to-remove" },
 "associationTypes": [
 { "associationCategory": "HUBSPOT_DEFINED", "associationTypeId": 279 }
 ]
}]

```

Test 3:

Test the Primary Association

`PUT https://api.hubapi.com/crm/v4/objects/contact/{contactId}/associations/company/{bestCompanyId}`

I’m sure there are other ways to test this, but it’s how I would start.

Best,

Jaycee
