APIs & Integrations

PBaxter
Mitwirkender/Mitwirkende

Custom code for getting all associations

lösung

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

 

0 Upvotes
1 Akzeptierte Lösung
Expertopinionsa
Lösung
Teilnehmer/-in

Custom code for getting all associations

lösung

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 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:

 

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!

Lösung in ursprünglichem Beitrag anzeigen

2 Antworten
Expertopinionsa
Lösung
Teilnehmer/-in

Custom code for getting all associations

lösung

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 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:

 

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!

PBaxter
Mitwirkender/Mitwirkende

Custom code for getting all associations

lösung

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