<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Custom code for getting all associations in APIs &amp; Integrations</title>
    <link>https://community.hubspot.com/t5/APIs-Integrations/Custom-code-for-getting-all-associations/m-p/807850#M64928</link>
    <description>&lt;P&gt;Thank you for taking the time to update that for me - works a treat&lt;/P&gt;</description>
    <pubDate>Thu, 15 Jun 2023 13:37:31 GMT</pubDate>
    <dc:creator>PBaxter</dc:creator>
    <dc:date>2023-06-15T13:37:31Z</dc:date>
    <item>
      <title>Custom code for getting all associations</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Custom-code-for-getting-all-associations/m-p/807192#M64878</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have some simple custom code that is grabbing company associations for a deal, and then getting some key data about the companies:&lt;/P&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;const hubspot = require('@hubspot/api-client');&lt;/DIV&gt;&lt;DIV&gt;exports.main = (event, callback) =&amp;gt; {&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; const hubspotClient = new hubspot.Client({&lt;SPAN&gt;accessToken: process.env.private_app_token&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; });&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;//First, make a call to get deal associations to companies&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; hubspotClient.crm.deals.associationsApi.getAll(event.object.objectId, 'Companies').then((results) =&amp;gt; {&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; let companyPromises = results.body.results.map(item =&amp;gt; {&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return hubspotClient.crm.companies.basicApi.getById(item.id, ['hs_object_id', 'type_of_company']).then(results =&amp;gt; {&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return results.body&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; })&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; })&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;All good, but with&amp;nbsp;HubSpot Client v3, this only returns companies with a 'Primary' association label.&amp;nbsp;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;How can I modify this to return all associations independent of label (including 'nulls')?&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;I managed to get it to work via the v4 Assocations API with path of&amp;nbsp;"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.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Thanks!&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Phil&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Jun 2023 11:54:37 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Custom-code-for-getting-all-associations/m-p/807192#M64878</guid>
      <dc:creator>PBaxter</dc:creator>
      <dc:date>2023-06-14T11:54:37Z</dc:date>
    </item>
    <item>
      <title>Re: Custom code for getting all associations</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Custom-code-for-getting-all-associations/m-p/807237#M64881</link>
      <description>&lt;P&gt;Hi Phil, Being straight to the point.. To modify your code and get all associations regardless of their labels, we can use the &lt;A href="https://triotechsystems.com/association-labels-in-hubspot-a-guide-for-developers/" target="_blank" rel="noopener"&gt;HubSpot Associations&lt;/A&gt; 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:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;const hubspot = require('@hubspot/api-client');

exports.main = (event, callback) =&amp;gt; {
    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 =&amp;gt; ({
            from: {
                id: objectId,
                type: 'deal'
            },
            to: {
                type: 'company'
            }
        }))
    }).then(results =&amp;gt; {
        let companyPromises = results.body.results.map(item =&amp;gt; {
            return hubspotClient.crm.companies.basicApi.getById(item.to.id, ['hs_object_id', 'type_of_company']).then(results =&amp;gt; {
                return results.body
            });
        });

        Promise.all(companyPromises).then(companies =&amp;gt; {
            callback(null, companies);
        }).catch(error =&amp;gt; {
            callback(error);
        });
    }).catch(error =&amp;gt; {
        callback(error);
    });
};&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Let me know if anything bthers you!&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Jun 2023 13:11:30 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Custom-code-for-getting-all-associations/m-p/807237#M64881</guid>
      <dc:creator>Expertopinionsa</dc:creator>
      <dc:date>2023-06-14T13:11:30Z</dc:date>
    </item>
    <item>
      <title>Re: Custom code for getting all associations</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Custom-code-for-getting-all-associations/m-p/807850#M64928</link>
      <description>&lt;P&gt;Thank you for taking the time to update that for me - works a treat&lt;/P&gt;</description>
      <pubDate>Thu, 15 Jun 2023 13:37:31 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Custom-code-for-getting-all-associations/m-p/807850#M64928</guid>
      <dc:creator>PBaxter</dc:creator>
      <dc:date>2023-06-15T13:37:31Z</dc:date>
    </item>
  </channel>
</rss>

