<?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: Associate contact to custom object based on matching property values in APIs &amp; Integrations</title>
    <link>https://community.hubspot.com/t5/APIs-Integrations/Associate-contact-to-custom-object-based-on-matching-property/m-p/882839#M69221</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/426475"&gt;@coldrickjack&lt;/a&gt;&amp;nbsp;thanks A MILLION for your help on this one &amp;amp; the explainer video!&lt;BR /&gt;&lt;BR /&gt;I added my hubspot token &amp;amp; had to exchange the /partner/ in the request URL with the custom object type ID; but it worked perfectly!&lt;BR /&gt;&lt;BR /&gt;I might need to take a look at removing the association if the ID changes (in your video, all three of them stay associated to the contact) but that's not super high prio at the moment.&lt;BR /&gt;&lt;BR /&gt;Thank you for sharing your code &amp;amp; help me (&amp;amp; others in the future!)&lt;/P&gt;</description>
    <pubDate>Tue, 21 Nov 2023 17:18:51 GMT</pubDate>
    <dc:creator>HaVdb</dc:creator>
    <dc:date>2023-11-21T17:18:51Z</dc:date>
    <item>
      <title>Associate contact to custom object based on matching property values</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Associate-contact-to-custom-object-based-on-matching-property/m-p/881063#M69170</link>
      <description>&lt;P&gt;Hi community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;We recently added a custom object for our referral &amp;amp; commission partners. This way, if a lead comes in via their specific trial links (we then capture their "partner_id" on contact level); we can link it to the custom object &amp;amp; it's clear for our internal sales team that there is already an association to a parter (so the internal sales doesn't have to work on the contact)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So: When a contact gets a value for "partner_id" by filling in a trial request; it will be triggered in a workflow. I then want a custom coded action to associate the contact to the right custom Partner object (based on a match of the partner_id property)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I found this custom code to associate a conact to a company --&amp;gt;&amp;nbsp;&lt;A href="https://gist.github.com/jackcoldrick90/faa4f25eb6f07a6bc50b84589a574b3d" target="_blank"&gt;https://gist.github.com/jackcoldrick90/faa4f25eb6f07a6bc50b84589a574b3d&lt;/A&gt;&amp;nbsp;from&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/5724"&gt;@jackcoldrick&lt;/a&gt;&amp;nbsp; but am trying to modify it to my needs.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was wondering if anyone would be able to help me out.&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/278677"&gt;@BenLerner-Paio&lt;/a&gt;&amp;nbsp;, could you help me out again by any chance?&lt;BR /&gt;&lt;BR /&gt;So, I have been able to find documentation &amp;amp; did some get requests to get to my object type Id &amp;amp; the type Id of the association of contact to partner object.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My custom object (referral_partner) had the custom object type id "2-18324712" and the type Id for the association should be 59&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Screenshot 2023-11-17 at 09.41.01.png" style="width: 400px;"&gt;&lt;img src="https://community.hubspot.com/t5/image/serverpage/image-id/103460i883856FA4646D6AE/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Screenshot 2023-11-17 at 09.41.01.png" alt="Screenshot 2023-11-17 at 09.41.01.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How do I modify the code from &lt;A href="https://gist.github.com/jackcoldrick90/faa4f25eb6f07a6bc50b84589a574b3d" target="_blank" rel="noopener"&gt;associateContactToCompany&lt;/A&gt;&lt;/P&gt;&lt;P&gt;in order for it to match the contents of the contact property "partner_id" to the contents of the custom object property "partner_id" &amp;amp; if there's a match -&amp;gt; associate the contact to the custom object?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I gave it a try by looking at the code &amp;amp; some other community stuff but it doesn't make sense.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;// Import HubSpot Client Library and instantiate client with private app token
const hubspot = require('@hubspot/api-client');
const hubspotClient = new hubspot.Client({
    accessToken: process.env.HannePartnerTest // Access token associated with your private app (also be sure to include token in secrets)
});

// Helper function to get the associated Object ID of a specified Object Type
async function getAssociatedObjectId(objectType, objectId, toObjectType) {
    let obj = await hubspotClient.crm.objects.associationsApi.getAll(objectType, objectId, toObjectType);
    return obj.results[0].id;
}

// Helper function to associate a custom object to a contact
async function associateCustomObjectToContact(objectTypeId, customObjectId, contactId) {
    const AssociationSpec = [{
        "associationCategory": "USER_DEFINED",
        "associationTypeId": 59, // Add a comma here
        "label": "Referral Contact"
    }];
    hubspotClient.apiRequest({
        method: 'put', // Use lowercase 'put'
        path: `/crm/v4/objects/${objectTypeId}/${customObjectId}/associations/contact/${contactId}`,
        body: AssociationSpec
    });
}

exports.main = async (event) =&amp;gt; {

    // Define variables
    const objectTypeId = '2-18324712'; // Custom Object "objectTypeId" (this will be different for you)
    const objectId = event.object.objectId // Currently enrolled Custom Object Record Id

    // Storing the partnerID into a const
    const partnerID = event.inputFields['partner_id'];

    // Create search criteria   
    const filter = { propertyName: 'partner_id', operator: 'EQ', value: partnerID };
    const filterGroup = { filters: [filter] };
    const sort = JSON.stringify({ propertyName: 'partner_id', direction: 'DESCENDING' });
    const properties = ['partner_id'];
    const limit = 1;
    const after = 0;

    const searchCriteria = {
        filterGroups: [filterGroup],
        sorts: [sort],
        properties,
        limit,
        after
    };

    // Search the CRM for custom objects matching "partnerID" variable defined earlier
    hubspotClient.crm.objects.searchApi.doSearch(objectTypeId, searchCriteria).then(searchResponse =&amp;gt; {
        // console.log("RESULTS: " + searchResponse.body.total); // - FOR DEBUG
        // Continue with the rest of your code...
    });
};
// if MATCH FOUND - ASSOCIATE partner object to CONTACT
  if(// console.log("PartnerID " + partnerId + " FOUND: ASSOCIATE RECORDS"); // - FOR DEBUG
          //Associate Company with Contact
           hubspotClient.crm.objects.associationsApi.create(searchResponse.body.results[0].id,'contacts', event.object.objectId,'associateCustomObjectToContact');
         }
      });
   
      callback({outputFields: {}});
    
    })
    .catch(err =&amp;gt; {
      console.error(err);
    });
})&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Nov 2023 09:19:46 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Associate-contact-to-custom-object-based-on-matching-property/m-p/881063#M69170</guid>
      <dc:creator>HaVdb</dc:creator>
      <dc:date>2023-11-17T09:19:46Z</dc:date>
    </item>
    <item>
      <title>Re: Associate contact to custom object based on matching property values</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Associate-contact-to-custom-object-based-on-matching-property/m-p/882262#M69189</link>
      <description>&lt;P&gt;Hey,&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/163978"&gt;@HaVdb&lt;/a&gt;&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":waving_hand:"&gt;👋&lt;/span&gt; Thanks for your question. Since this is code from a repo created by&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/426475"&gt;@coldrickjack&lt;/a&gt;, you might try opening an issue there too.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Have you tried breaking this down a bit and testing the individual calls using Postman? Verifying the calls work as expected in a simplified manner is often useful.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Additionally, have you tried adding some debugging to your code? Adding a few console.log() statements could help reveal any issues.&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;After fetching the response(s)&lt;/LI&gt;
&lt;LI&gt;After searching for the custom object&lt;/LI&gt;
&lt;LI&gt;Before creating the associations&lt;/LI&gt;
&lt;LI&gt;Adding one to your catch block&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Tracking down the flow of data is a great place to start in debugging your code customization.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Have fun testing! — Jaycee&lt;/P&gt;</description>
      <pubDate>Mon, 20 Nov 2023 18:48:50 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Associate-contact-to-custom-object-based-on-matching-property/m-p/882262#M69189</guid>
      <dc:creator>Jaycee_Lewis</dc:creator>
      <dc:date>2023-11-20T18:48:50Z</dc:date>
    </item>
    <item>
      <title>Re: Associate contact to custom object based on matching property values</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Associate-contact-to-custom-object-based-on-matching-property/m-p/882524#M69204</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/163978"&gt;@HaVdb&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think we should easily be able to modify this code to support your use case. Would you mind letting me know if the partner_id property is storing a numeric or text value as this will influence how we look up the appropriate object to associate.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Nov 2023 08:32:22 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Associate-contact-to-custom-object-based-on-matching-property/m-p/882524#M69204</guid>
      <dc:creator>coldrickjack</dc:creator>
      <dc:date>2023-11-21T08:32:22Z</dc:date>
    </item>
    <item>
      <title>Re: Associate contact to custom object based on matching property values</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Associate-contact-to-custom-object-based-on-matching-property/m-p/882534#M69205</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/426475"&gt;@coldrickjack&lt;/a&gt;&amp;nbsp;,&lt;BR /&gt;&lt;BR /&gt;on contact level it's a single line text property; on the partner level it's a number property. But I can still change this if need be, I only have 8 values currently (we haven't migrated all our partners in HubSpot just yet).&lt;/P&gt;</description>
      <pubDate>Tue, 21 Nov 2023 09:06:21 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Associate-contact-to-custom-object-based-on-matching-property/m-p/882534#M69205</guid>
      <dc:creator>HaVdb</dc:creator>
      <dc:date>2023-11-21T09:06:21Z</dc:date>
    </item>
    <item>
      <title>Re: Associate contact to custom object based on matching property values</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Associate-contact-to-custom-object-based-on-matching-property/m-p/882817#M69220</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/163978"&gt;@HaVdb&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think this &lt;A href="https://gist.github.com/jackcoldrick90/a6c55362dbece024e380beb19c65274d" target="_blank" rel="noopener"&gt;code&lt;/A&gt; will work for you. Let me know how you get on. I've also recorded a short &lt;A href="https://share.vidyard.com/watch/j9hUUPYB7YbfH39JZ4UMFd?" target="_blank" rel="noopener"&gt;video&lt;/A&gt; running through how it all works.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;All the best,&lt;/P&gt;
&lt;P&gt;Jack&lt;/P&gt;</description>
      <pubDate>Tue, 21 Nov 2023 16:56:42 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Associate-contact-to-custom-object-based-on-matching-property/m-p/882817#M69220</guid>
      <dc:creator>coldrickjack</dc:creator>
      <dc:date>2023-11-21T16:56:42Z</dc:date>
    </item>
    <item>
      <title>Re: Associate contact to custom object based on matching property values</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Associate-contact-to-custom-object-based-on-matching-property/m-p/882839#M69221</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/426475"&gt;@coldrickjack&lt;/a&gt;&amp;nbsp;thanks A MILLION for your help on this one &amp;amp; the explainer video!&lt;BR /&gt;&lt;BR /&gt;I added my hubspot token &amp;amp; had to exchange the /partner/ in the request URL with the custom object type ID; but it worked perfectly!&lt;BR /&gt;&lt;BR /&gt;I might need to take a look at removing the association if the ID changes (in your video, all three of them stay associated to the contact) but that's not super high prio at the moment.&lt;BR /&gt;&lt;BR /&gt;Thank you for sharing your code &amp;amp; help me (&amp;amp; others in the future!)&lt;/P&gt;</description>
      <pubDate>Tue, 21 Nov 2023 17:18:51 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Associate-contact-to-custom-object-based-on-matching-property/m-p/882839#M69221</guid>
      <dc:creator>HaVdb</dc:creator>
      <dc:date>2023-11-21T17:18:51Z</dc:date>
    </item>
    <item>
      <title>Re: Associate contact to custom object based on matching property values</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Associate-contact-to-custom-object-based-on-matching-property/m-p/883087#M69240</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/127074"&gt;@Jaycee_Lewis&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I did try some of it; but I'm a total newbie at coding so I have been trying to make sense of it (also tried to ask ChatGPT to help me out a little) but didn't seem to be able to fully figure it out.&lt;BR /&gt;&lt;BR /&gt;That's why I thought it could be good to ask the original author of the code how I could make it work for my case &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;I'll definitely take your tips with me for a next time. Thanks!&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":smiling_face_with_smiling_eyes:"&gt;😊&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Nov 2023 08:33:56 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Associate-contact-to-custom-object-based-on-matching-property/m-p/883087#M69240</guid>
      <dc:creator>HaVdb</dc:creator>
      <dc:date>2023-11-22T08:33:56Z</dc:date>
    </item>
    <item>
      <title>Re: Associate contact to custom object based on matching property values</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Associate-contact-to-custom-object-based-on-matching-property/m-p/884480#M69299</link>
      <description>&lt;P&gt;Glad it was helpful&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/163978"&gt;@HaVdb&lt;/a&gt;&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":smiling_face_with_smiling_eyes:"&gt;😊&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 25 Nov 2023 15:22:50 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Associate-contact-to-custom-object-based-on-matching-property/m-p/884480#M69299</guid>
      <dc:creator>coldrickjack</dc:creator>
      <dc:date>2023-11-25T15:22:50Z</dc:date>
    </item>
    <item>
      <title>Re: Associate contact to custom object based on matching property values</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Associate-contact-to-custom-object-based-on-matching-property/m-p/913283#M70656</link>
      <description>&lt;P&gt;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/426475"&gt;@coldrickjack&lt;/a&gt;&amp;nbsp;I was wondering if you could shed some light on an issue related to this initial question.&lt;BR /&gt;&lt;BR /&gt;I managed to adjust your code to also add an association label to the contact ("referral contact"); but got the question internally to add another association label on top of the initial one.&lt;BR /&gt;&lt;BR /&gt;When I adjusted the code; it just overwrites the label value. I can't seem to add 2 association labels at the same time with the workflow.&lt;BR /&gt;&lt;BR /&gt;Do you know if this is possible?&lt;BR /&gt;&lt;BR /&gt;I tested it with the code below, but it only associates the last label. I feel like it overwrites the first value with the last value.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Have you come across a similar issue by any chance?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for you time!&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":folded_hands:"&gt;🙏&lt;/span&gt;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;const axios = require('axios');
axios.defaults.headers.common['authorization'] = `Bearer ${process.env.HannePartnerTest}`;
axios.defaults.headers.common['Content-Type'] = `application/json`;
axios.defaults.headers.put['Content-Type'] = `application/json`;

exports.main = async (event) =&amp;gt; {
    const partner_id = event.inputFields['partner_id'];

    const partnerObjectId = await getPartner(partner_id);
    console.log(partnerObjectId.results[0].id);

    const contact_id = event["object"]["objectId"];

    const associateResult = await associateContactToPartner(contact_id, partnerObjectId.results[0].id);
    console.log(associateResult);

    // Check if the association was successful before associating the labels
    if (associateResult) {
        const label1Result = await associateLabelToContact(contact_id, partnerObjectId.results[0].id, 59);
        console.log(label1Result);

        const label2Result = await associateLabelToContact(contact_id, partnerObjectId.results[0].id, 68);
        console.log(label2Result);
    }
}

// 1. Get HubSpot Partner using Contact Partner ID value
function getPartner(partner_id) {
    let requestUrl = `https://api.hubapi.com/crm/v3/objects/2-18324712/search`;
    let data = {
        filterGroups: [{
            filters: [{
                propertyName: 'partner_id',
                operator: 'EQ',
                value: partner_id
            }]
        }],
        properties: ['name', 'partner_id'],
    }
    return new Promise((resolve) =&amp;gt; {
        axios.post(requestUrl, data).then(res =&amp;gt; {
            resolve(res.data);
        }).catch(err =&amp;gt; {
            console.log(err.message)
        });
    });
}

// 2. Associate Contact to Partner Object
function associateContactToPartner(contact_id, partner_id) {
    // Request body for association with labels 59 and 68
    const associationSpec = [
        { "associationCategory": "USER_DEFINED", "associationTypeId": 59 },
        { "associationCategory": "USER_DEFINED", "associationTypeId": 68 }
    ];

    let requestUrl = `https://api.hubapi.com/crm/v4/objects/contacts/${contact_id}/associations/default/2-18324712/${partner_id}`;
    console.log(requestUrl);

    return new Promise((resolve) =&amp;gt; {
        axios.put(requestUrl, associationSpec).then(res =&amp;gt; {
            resolve(res.data);
        }).catch(err =&amp;gt; {
            console.log(err.message)
            resolve(null); // Resolve with null to indicate failure
        });
    });
}

// 3. Associate Label to Contact
function associateLabelToContact(contact_id, partner_id, associationTypeId) {
    // Request body for association with a label
    const associationSpec = [{
        "associationCategory": "USER_DEFINED",
        "associationTypeId": associationTypeId
    }];

    let requestUrl = `https://api.hubapi.com/crm/v4/objects/0-1/${contact_id}/associations/2-18324712/${partner_id}`;
    console.log(requestUrl);

    return new Promise((resolve) =&amp;gt; {
        axios.put(requestUrl, associationSpec).then(res =&amp;gt; {
            resolve(res.data);
        }).catch(err =&amp;gt; {
            console.log(err.message)
            resolve(null); // Resolve with null to indicate failure
        });
    });
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Jan 2024 16:37:04 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Associate-contact-to-custom-object-based-on-matching-property/m-p/913283#M70656</guid>
      <dc:creator>HaVdb</dc:creator>
      <dc:date>2024-01-29T16:37:04Z</dc:date>
    </item>
  </channel>
</rss>

