# Custom code for getting all associations

**URL:** <https://community.hubspot.com/t/custom-code-for-getting-all-associations/83371>\
**Category:** APIs & Integrations\
**Tags:** companies\
**Created:** [June 14, 2023, 11:54am UTC](https://community.hubspot.com/t/custom-code-for-getting-all-associations/83371 "2023-06-14T11:54:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![PBaxter](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/pbaxter/32/86357_2.png) [@PBaxter](https://community.hubspot.com/u/PBaxter)\
**Post date:** [June 14, 2023, 11:54am UTC](https://community.hubspot.com/t/custom-code-for-getting-all-associations/83371/1 "2023-06-14T11:54:37Z")

</div>

Hi,

I have some simple custom code that is grabbing company associations for a deal, and then getting some key data about the companies:

const hubspot = require(‘@hubspot/api-client’);

exports.main = (event, callback) =\> {

const hubspotClient = new hubspot.Client({accessToken: process.env.private\_app\_token

});

//First, make a call to get deal associations to companies

hubspotClient.crm.deals.associationsApi.getAll(event.object.objectId, ‘Companies’).then((results) =\> {

let companyPromises = results.body.results.map(item =\> {

return hubspotClient.crm.companies.basicApi.getById(item.id, [‘hs\_object\_id’, ‘type\_of\_company’]).then(results =\> {

return results.body

})

})

All good, but with HubSpot Client v3, this only returns companies with a ‘Primary’ association label.

How can I modify this to return all associations independent of label (including ‘nulls’)?

I managed to get it to work via the v4 Assocations API with path of “path”: “/crm/v4/associations/deal/company/batch/read” but I can’t find any guidance on how to make that call through the custom code window.

Thanks!

Phil

---

<div class="post-metadata">

**Author:** ![Expertopinionsa](https://avatars.discourse-cdn.com/v4/letter/e/a183cd/32.png) [@Expertopinionsa](https://community.hubspot.com/u/Expertopinionsa)\
**Post date:** [June 14, 2023, 1:11pm UTC](https://community.hubspot.com/t/custom-code-for-getting-all-associations/83371/2 "2023-06-14T13:11:30Z")

</div>

Hi Phil, Being straight to the point.. To modify your code and get all associations regardless of their labels, we can use the [HubSpot Associations](https://triotechsystems.com/association-labels-in-hubspot-a-guide-for-developers/) API in version 4. If you have installed and set up the required components properly, you should run this code in an environment that can handle HubSpot API calls. Below is an updated version of your code that fetches all associations using the v4 Associations API:

```lia-code-sample
const hubspot = require('@hubspot/api-client');

exports.main = (event, callback) => {
 const hubspotClient = new hubspot.Client({ accessToken: process.env.private_app_token });

 // First, make a call to get deal associations to companies
 hubspotClient.crm.v4.associationsApi.readBatch({
 inputs: event.object.objectId.map(objectId => ({
 from: {
 id: objectId,
 type: 'deal'
 },
 to: {
 type: 'company'
 }
 }))
 }).then(results => {
 let companyPromises = results.body.results.map(item => {
 return hubspotClient.crm.companies.basicApi.getById(item.to.id, ['hs_object_id', 'type_of_company']).then(results => {
 return results.body
 });
 });

 Promise.all(companyPromises).then(companies => {
 callback(null, companies);
 }).catch(error => {
 callback(error);
 });
 }).catch(error => {
 callback(error);
 });
};

```

Let me know if anything bthers you!

---

<div class="post-metadata">

**Author:** ![PBaxter](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/pbaxter/32/86357_2.png) [@PBaxter](https://community.hubspot.com/u/PBaxter)\
**Post date:** [June 15, 2023, 1:37pm UTC](https://community.hubspot.com/t/custom-code-for-getting-all-associations/83371/3 "2023-06-15T13:37:31Z")

</div>

Thank you for taking the time to update that for me - works a treat
