<?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 Associating Contacts &amp;amp; Companies to a Custom Object in APIs &amp; Integrations</title>
    <link>https://community.hubspot.com/t5/APIs-Integrations/Associating-Contacts-amp-Companies-to-a-Custom-Object/m-p/988796#M74171</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to use a workflow to associate a deal's contact and company to the custom object that is also assocated to the deal.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The custom object is a inventory object called, Available Inventories, which has a object ID of "2-24924286".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I want to happen is when the deal is Closed Won, the Inventory object gets associated to the contact and company so our Delivery Team can execute.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the code I'm using:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;const axios = require('axios');

// Retrieve the API key from the secrets manager
const apiKey = process.env.HUBSPOT_API_KEY;

// Main function to handle the workflow
exports.main = async (event, callback) =&amp;gt; {
    // Input fields from the workflow
    const customObjectId = event.object.objectId; // The ID of the Available Inventory custom object
    const associatedDealId = event.inputFields.associatedDealRecordId; // The deal ID from the custom object's property

    if (!associatedDealId) {
        callback({
            error: "associatedDealRecordId is undefined"
        });
        return;
    }

    console.log(`Custom Object ID: ${customObjectId}`);
    console.log(`Associated Deal ID: ${associatedDealId}`);

    // Function to get associated objects
    const getAssociations = async (objectType, objectId, associationType) =&amp;gt; {
        const url = `https://api.hubapi.com/crm/v3/objects/${objectType}/${objectId}/associations/${associationType}`;
        try {
            const response = await axios.get(url, {
                headers: {
                    Authorization: `Bearer ${apiKey}`
                }
            });
            console.log(`Associations for ${objectType} ID ${objectId}:`, response.data.results);
            return response.data.results;
        } catch (error) {
            console.error(`Error fetching associations for ${objectType} ID ${objectId}: ${error.message}`);
            throw error;
        }
    };

    // Function to create associations
    const createAssociation = async (fromObjectType, fromObjectId, toObjectType, toObjectId) =&amp;gt; {
        const url = `https://api.hubapi.com/crm/v3/associations/v1/${fromObjectType}/${fromObjectId}/contacts/${toObjectId}`;
        console.log(`Creating association with URL: ${url}`);
        try {
            await axios.put(url, {}, {
                headers: {
                    Authorization: `Bearer ${apiKey}`
                }
            });
            console.log(`Created association between ${fromObjectType} ID ${fromObjectId} and contacts ID ${toObjectId}`);
        } catch (error) {
            console.error(`Error creating association between ${fromObjectType} ID ${fromObjectId} and contacts ID ${toObjectId}: ${error.message}`);
            console.error(`URL: ${url}`);
            throw error;
        }
    };

    try {
        // Get associated contacts from the deal
        const contacts = await getAssociations('deals', associatedDealId, 'contacts');
        
        // Get associated companies from the deal
        const companies = await getAssociations('deals', associatedDealId, 'companies');

        // Associate contacts to Available Inventory custom object
        for (const contact of contacts) {
            await createAssociation('2-24924286', customObjectId, 'contacts', contact.id);
        }

        // Associate companies to Available Inventory custom object
        for (const company of companies) {
            await createAssociation('2-24924286', customObjectId, 'companies', company.id);
        }

        callback({
            outputFields: {
                message: "Associations created successfully"
            }
        });
    } catch (error) {
        console.error(`Workflow error: ${error.message}`);
        callback({
            error: error.message
        });
    }
};&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I test it, I get keep getting a 404 error message for the API call. Am I using the wrong call?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've tried switching out&amp;nbsp; await createAssociation('2-24924286', customObjectId, 'companies', company.id); for&amp;nbsp; await createAssociation('availableinventories', customObjectId, 'companies', company.id); and no luck either.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the log I get when I test:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Logs
2024-06-07T13:09:13.405Z	INFO	Custom Object ID: 12986399368
2024-06-07T13:09:13.442Z	INFO	Associated Deal ID: 18850560338
2024-06-07T13:09:14.545Z	INFO	Associations for deals ID 18850560338: [
  { id: '81948', type: 'deal_to_contact' },
  { id: '6822394764', type: 'deal_to_contact' },
  { id: '15152765442', type: 'deal_to_contact' },
  { id: '15225208649', type: 'deal_to_contact' },
  { id: '16153499199', type: 'deal_to_contact' }
]
2024-06-07T13:09:14.681Z	INFO	Associations for deals ID 18850560338: [ { id: '14879856903', type: 'deal_to_company' } ]
2024-06-07T13:09:14.682Z	INFO	Creating association with URL: https://api.hubapi.com/crm/v3/associations/v1/2-24924286/12986399368/contacts/81948
2024-06-07T13:09:15.541Z	ERROR	Error creating association between 2-24924286 ID 12986399368 and contacts ID 81948: Request failed with status code 404
2024-06-07T13:09:15.541Z	ERROR	URL: https://api.hubapi.com/crm/v3/associations/v1/2-24924286/12986399368/contacts/81948
2024-06-07T13:09:15.541Z	ERROR	Workflow error: Request failed with status code 404

Memory: 85/128 MB
Runtime: 2159.01 ms&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2024-06-07 at 9.05.57 AM.png" style="width: 901px;"&gt;&lt;img src="https://community.hubspot.com/t5/image/serverpage/image-id/119263i279ED57F7A3DAD5A/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot 2024-06-07 at 9.05.57 AM.png" alt="Screenshot 2024-06-07 at 9.05.57 AM.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2024-06-07 at 9.11.07 AM.png" style="width: 999px;"&gt;&lt;img src="https://community.hubspot.com/t5/image/serverpage/image-id/119264iD8DF400F2CA342EC/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot 2024-06-07 at 9.11.07 AM.png" alt="Screenshot 2024-06-07 at 9.11.07 AM.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2024-06-07 at 9.11.22 AM.png" style="width: 999px;"&gt;&lt;img src="https://community.hubspot.com/t5/image/serverpage/image-id/119265iA7EBF3BF273F0913/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot 2024-06-07 at 9.11.22 AM.png" alt="Screenshot 2024-06-07 at 9.11.22 AM.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2024-06-07 at 9.11.45 AM.png" style="width: 999px;"&gt;&lt;img src="https://community.hubspot.com/t5/image/serverpage/image-id/119266iA5FDFF3C175CC15B/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot 2024-06-07 at 9.11.45 AM.png" alt="Screenshot 2024-06-07 at 9.11.45 AM.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 07 Jun 2024 13:47:11 GMT</pubDate>
    <dc:creator>JonahWilson</dc:creator>
    <dc:date>2024-06-07T13:47:11Z</dc:date>
    <item>
      <title>Associating Contacts &amp; Companies to a Custom Object</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Associating-Contacts-amp-Companies-to-a-Custom-Object/m-p/988796#M74171</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to use a workflow to associate a deal's contact and company to the custom object that is also assocated to the deal.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The custom object is a inventory object called, Available Inventories, which has a object ID of "2-24924286".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I want to happen is when the deal is Closed Won, the Inventory object gets associated to the contact and company so our Delivery Team can execute.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the code I'm using:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;const axios = require('axios');

// Retrieve the API key from the secrets manager
const apiKey = process.env.HUBSPOT_API_KEY;

// Main function to handle the workflow
exports.main = async (event, callback) =&amp;gt; {
    // Input fields from the workflow
    const customObjectId = event.object.objectId; // The ID of the Available Inventory custom object
    const associatedDealId = event.inputFields.associatedDealRecordId; // The deal ID from the custom object's property

    if (!associatedDealId) {
        callback({
            error: "associatedDealRecordId is undefined"
        });
        return;
    }

    console.log(`Custom Object ID: ${customObjectId}`);
    console.log(`Associated Deal ID: ${associatedDealId}`);

    // Function to get associated objects
    const getAssociations = async (objectType, objectId, associationType) =&amp;gt; {
        const url = `https://api.hubapi.com/crm/v3/objects/${objectType}/${objectId}/associations/${associationType}`;
        try {
            const response = await axios.get(url, {
                headers: {
                    Authorization: `Bearer ${apiKey}`
                }
            });
            console.log(`Associations for ${objectType} ID ${objectId}:`, response.data.results);
            return response.data.results;
        } catch (error) {
            console.error(`Error fetching associations for ${objectType} ID ${objectId}: ${error.message}`);
            throw error;
        }
    };

    // Function to create associations
    const createAssociation = async (fromObjectType, fromObjectId, toObjectType, toObjectId) =&amp;gt; {
        const url = `https://api.hubapi.com/crm/v3/associations/v1/${fromObjectType}/${fromObjectId}/contacts/${toObjectId}`;
        console.log(`Creating association with URL: ${url}`);
        try {
            await axios.put(url, {}, {
                headers: {
                    Authorization: `Bearer ${apiKey}`
                }
            });
            console.log(`Created association between ${fromObjectType} ID ${fromObjectId} and contacts ID ${toObjectId}`);
        } catch (error) {
            console.error(`Error creating association between ${fromObjectType} ID ${fromObjectId} and contacts ID ${toObjectId}: ${error.message}`);
            console.error(`URL: ${url}`);
            throw error;
        }
    };

    try {
        // Get associated contacts from the deal
        const contacts = await getAssociations('deals', associatedDealId, 'contacts');
        
        // Get associated companies from the deal
        const companies = await getAssociations('deals', associatedDealId, 'companies');

        // Associate contacts to Available Inventory custom object
        for (const contact of contacts) {
            await createAssociation('2-24924286', customObjectId, 'contacts', contact.id);
        }

        // Associate companies to Available Inventory custom object
        for (const company of companies) {
            await createAssociation('2-24924286', customObjectId, 'companies', company.id);
        }

        callback({
            outputFields: {
                message: "Associations created successfully"
            }
        });
    } catch (error) {
        console.error(`Workflow error: ${error.message}`);
        callback({
            error: error.message
        });
    }
};&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I test it, I get keep getting a 404 error message for the API call. Am I using the wrong call?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've tried switching out&amp;nbsp; await createAssociation('2-24924286', customObjectId, 'companies', company.id); for&amp;nbsp; await createAssociation('availableinventories', customObjectId, 'companies', company.id); and no luck either.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the log I get when I test:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Logs
2024-06-07T13:09:13.405Z	INFO	Custom Object ID: 12986399368
2024-06-07T13:09:13.442Z	INFO	Associated Deal ID: 18850560338
2024-06-07T13:09:14.545Z	INFO	Associations for deals ID 18850560338: [
  { id: '81948', type: 'deal_to_contact' },
  { id: '6822394764', type: 'deal_to_contact' },
  { id: '15152765442', type: 'deal_to_contact' },
  { id: '15225208649', type: 'deal_to_contact' },
  { id: '16153499199', type: 'deal_to_contact' }
]
2024-06-07T13:09:14.681Z	INFO	Associations for deals ID 18850560338: [ { id: '14879856903', type: 'deal_to_company' } ]
2024-06-07T13:09:14.682Z	INFO	Creating association with URL: https://api.hubapi.com/crm/v3/associations/v1/2-24924286/12986399368/contacts/81948
2024-06-07T13:09:15.541Z	ERROR	Error creating association between 2-24924286 ID 12986399368 and contacts ID 81948: Request failed with status code 404
2024-06-07T13:09:15.541Z	ERROR	URL: https://api.hubapi.com/crm/v3/associations/v1/2-24924286/12986399368/contacts/81948
2024-06-07T13:09:15.541Z	ERROR	Workflow error: Request failed with status code 404

Memory: 85/128 MB
Runtime: 2159.01 ms&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2024-06-07 at 9.05.57 AM.png" style="width: 901px;"&gt;&lt;img src="https://community.hubspot.com/t5/image/serverpage/image-id/119263i279ED57F7A3DAD5A/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot 2024-06-07 at 9.05.57 AM.png" alt="Screenshot 2024-06-07 at 9.05.57 AM.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2024-06-07 at 9.11.07 AM.png" style="width: 999px;"&gt;&lt;img src="https://community.hubspot.com/t5/image/serverpage/image-id/119264iD8DF400F2CA342EC/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot 2024-06-07 at 9.11.07 AM.png" alt="Screenshot 2024-06-07 at 9.11.07 AM.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2024-06-07 at 9.11.22 AM.png" style="width: 999px;"&gt;&lt;img src="https://community.hubspot.com/t5/image/serverpage/image-id/119265iA7EBF3BF273F0913/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot 2024-06-07 at 9.11.22 AM.png" alt="Screenshot 2024-06-07 at 9.11.22 AM.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2024-06-07 at 9.11.45 AM.png" style="width: 999px;"&gt;&lt;img src="https://community.hubspot.com/t5/image/serverpage/image-id/119266iA5FDFF3C175CC15B/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot 2024-06-07 at 9.11.45 AM.png" alt="Screenshot 2024-06-07 at 9.11.45 AM.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jun 2024 13:47:11 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Associating-Contacts-amp-Companies-to-a-Custom-Object/m-p/988796#M74171</guid>
      <dc:creator>JonahWilson</dc:creator>
      <dc:date>2024-06-07T13:47:11Z</dc:date>
    </item>
    <item>
      <title>Re: Associating Contacts &amp; Companies to a Custom Object</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Associating-Contacts-amp-Companies-to-a-Custom-Object/m-p/988901#M74175</link>
      <description>&lt;P&gt;Hey, &lt;SPAN style="background: var(--ck-color-mention-background); color: var(--ck-color-mention-text);"&gt;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/739016"&gt;@JonahWilson&lt;/a&gt;&lt;/SPAN&gt; I fished this one out of the spam filter &lt;span class="lia-unicode-emoji" title=":fishing_pole:"&gt;🎣&lt;/span&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Questions:&lt;/P&gt;
&lt;UL&gt;
 &lt;LI&gt;have you tried breaking this into a simpler problem for testing? Maybe set up a test call using Postman just to verify you can hit the endpoint successfully?&lt;/LI&gt;
 &lt;LI&gt;for your specific use case, is there a reason you prefer the v3 Associations endpoint vs. using the &lt;A href="https://developers.hubspot.com/docs/api/crm/associations" target="_blank"&gt;v4 endpoints&lt;/A&gt;?&lt;/LI&gt;
 &lt;LI&gt;can you check your URL structure for the `create associations` request? I wonder if the v1 in the URL is causing an issue&amp;nbsp;&lt;BR /&gt;&lt;A href="https://api.hubapi.com/crm/v3/associations/v1/2-24924286/12986399368/contacts/81948" target="_blank"&gt;https://api.hubapi.com/crm/v3/associations/&lt;STRONG&gt;v1&lt;/STRONG&gt;/2-24924286/12986399368/contacts/81948&lt;/A&gt;&lt;/LI&gt;
 &lt;LI&gt;it looks like you use using PUT with the v3 endpoint, &lt;A href="https://developers.hubspot.com/docs/api/crm/associations/v3#:~:text=records%20in%20HubSpot%2C-,make%20a%20POST%20request%20to,-/crm/v3/associations" target="_blank"&gt;but v3 uses POST&lt;/A&gt; whereas the&lt;A href="https://developers.hubspot.com/docs/api/crm/associations#:~:text=between%20two%20records%2C-,make%20a%20PUT%20request,-to%20/crm/v4" target="_blank"&gt; v4 uses PUT&lt;/A&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Thanks for the additional details! — Jaycee&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jun 2024 16:45:43 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Associating-Contacts-amp-Companies-to-a-Custom-Object/m-p/988901#M74175</guid>
      <dc:creator>Jaycee_Lewis</dc:creator>
      <dc:date>2024-06-07T16:45:43Z</dc:date>
    </item>
  </channel>
</rss>

