<?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 Dedupe Contacts with Programmable Automations in CMS Development</title>
    <link>https://community.hubspot.com/t5/CMS-Development/Dedupe-Contacts-with-Programmable-Automations/m-p/770729#M33389</link>
    <description>&lt;P&gt;Hi,&lt;BR /&gt;I am trying to set up a workflow with custom code to automatically deduplicate and merge contacts. See my code below. However, for some reason I am getting an error: " ERROR Unhandled Promise Rejection{"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"TypeError: Cannot read properties of undefined (reading 'properties')"&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;const DEDUPE_PROPERTY = 'firstname';
const hubspot = require('@hubspot/api-client');

exports.main = (event, callback) =&amp;gt; {
  // Make sure to add your API key under "Secrets" above.
  const hubspotClient = new hubspot.Client({
    accessToken: process.env.secretName
  });

  hubspotClient.crm.contacts.basicApi
    .getById(event.object.objectId, [DEDUPE_PROPERTY])
    .then(contactResult =&amp;gt; {
      let dedupePropValue = contactResult.body.properties[DEDUPE_PROPERTY];

      console.log(`Looking for duplicates based on ${DEDUPE_PROPERTY} = ${dedupePropValue}`);
      hubspotClient.crm.contacts.searchApi
        .doSearch({
          filterGroups: [{
            filters: [{
              propertyName: DEDUPE_PROPERTY,
              operator: 'EQ',
              value: dedupePropValue
            }]
          }]
        })
        .then(searchResults =&amp;gt; {
          let idsToMerge = searchResults.body.results
            .map(object =&amp;gt; object.id)
            .filter(vid =&amp;gt; Number(vid) !== Number(event.object.objectId));

          if (idsToMerge.length == 0) {
            console.log('No matching contact, nothing to merge');
            return;
          } else if (idsToMerge.length &amp;gt; 2) {
            console.log(`Found multiple potential contact IDs ${idsToMerge.join(', ')} to merge`);
            throw new Error("Ambiguous merge; more than one matching contact");
          }

          let idToMerge = idsToMerge[0];
          console.log(`Merging enrolled contact id=${event.object.objectId} into contact id=${idToMerge}`);
          hubspotClient
            .apiRequest({
              method: 'POST',
              path: `/contacts/v1/contact/merge-vids/${idToMerge}`,
              body: {
                vidToMerge: event.object.objectId
              }
            })
            .then(mergeResult =&amp;gt; {
              console.log('Contacts merged!');
            });
        });
    });
};&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 19 Mar 2023 03:31:40 GMT</pubDate>
    <dc:creator>GKostadinova</dc:creator>
    <dc:date>2023-03-19T03:31:40Z</dc:date>
    <item>
      <title>Dedupe Contacts with Programmable Automations</title>
      <link>https://community.hubspot.com/t5/CMS-Development/Dedupe-Contacts-with-Programmable-Automations/m-p/770729#M33389</link>
      <description>&lt;P&gt;Hi,&lt;BR /&gt;I am trying to set up a workflow with custom code to automatically deduplicate and merge contacts. See my code below. However, for some reason I am getting an error: " ERROR Unhandled Promise Rejection{"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"TypeError: Cannot read properties of undefined (reading 'properties')"&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;const DEDUPE_PROPERTY = 'firstname';
const hubspot = require('@hubspot/api-client');

exports.main = (event, callback) =&amp;gt; {
  // Make sure to add your API key under "Secrets" above.
  const hubspotClient = new hubspot.Client({
    accessToken: process.env.secretName
  });

  hubspotClient.crm.contacts.basicApi
    .getById(event.object.objectId, [DEDUPE_PROPERTY])
    .then(contactResult =&amp;gt; {
      let dedupePropValue = contactResult.body.properties[DEDUPE_PROPERTY];

      console.log(`Looking for duplicates based on ${DEDUPE_PROPERTY} = ${dedupePropValue}`);
      hubspotClient.crm.contacts.searchApi
        .doSearch({
          filterGroups: [{
            filters: [{
              propertyName: DEDUPE_PROPERTY,
              operator: 'EQ',
              value: dedupePropValue
            }]
          }]
        })
        .then(searchResults =&amp;gt; {
          let idsToMerge = searchResults.body.results
            .map(object =&amp;gt; object.id)
            .filter(vid =&amp;gt; Number(vid) !== Number(event.object.objectId));

          if (idsToMerge.length == 0) {
            console.log('No matching contact, nothing to merge');
            return;
          } else if (idsToMerge.length &amp;gt; 2) {
            console.log(`Found multiple potential contact IDs ${idsToMerge.join(', ')} to merge`);
            throw new Error("Ambiguous merge; more than one matching contact");
          }

          let idToMerge = idsToMerge[0];
          console.log(`Merging enrolled contact id=${event.object.objectId} into contact id=${idToMerge}`);
          hubspotClient
            .apiRequest({
              method: 'POST',
              path: `/contacts/v1/contact/merge-vids/${idToMerge}`,
              body: {
                vidToMerge: event.object.objectId
              }
            })
            .then(mergeResult =&amp;gt; {
              console.log('Contacts merged!');
            });
        });
    });
};&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 19 Mar 2023 03:31:40 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/Dedupe-Contacts-with-Programmable-Automations/m-p/770729#M33389</guid>
      <dc:creator>GKostadinova</dc:creator>
      <dc:date>2023-03-19T03:31:40Z</dc:date>
    </item>
    <item>
      <title>Re: Dedupe Contacts with Programmable Automations</title>
      <link>https://community.hubspot.com/t5/CMS-Development/Dedupe-Contacts-with-Programmable-Automations/m-p/770927#M33392</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/242491"&gt;@GKostadinova&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;The error you're seeing suggests that the &lt;CODE&gt;contactResult.body&lt;/CODE&gt; object is undefined, which means that there is no &lt;CODE&gt;properties&lt;/CODE&gt; property to read from it. This could happen if the &lt;CODE&gt;contactResult&lt;/CODE&gt; object is not properly formed, or if the API call to &lt;CODE&gt;hubspotClient.crm.contacts.basicApi.getById&lt;/CODE&gt; is failing for some reason.&lt;/P&gt;
&lt;P&gt;One possible solution is to add error handling to your code to catch any errors that occur during the API calls. &lt;BR /&gt;&lt;BR /&gt;You can do this by adding a &lt;CODE&gt;.catch()&lt;/CODE&gt; block to each of your API calls, like this:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;hubspotClient.crm.contacts.basicApi
    .getById(event.object.objectId, [DEDUPE_PROPERTY])
    .then(contactResult =&amp;gt; {
        // ...
    })
    .catch(err =&amp;gt; {
        console.error('Error getting contact by ID:', err);
        callback(err);
    });
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This will log any errors that occur to the console and pass the error back to the calling function using the &lt;CODE&gt;callback&lt;/CODE&gt; function.&lt;/P&gt;
&lt;P&gt;You can add similar error handling to the other API calls in your code to make it more robust and easier to debug.&lt;/P&gt;</description>
      <pubDate>Mon, 20 Mar 2023 08:10:57 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/Dedupe-Contacts-with-Programmable-Automations/m-p/770927#M33392</guid>
      <dc:creator>himanshurauthan</dc:creator>
      <dc:date>2023-03-20T08:10:57Z</dc:date>
    </item>
    <item>
      <title>Re: Dedupe Contacts with Programmable Automations</title>
      <link>https://community.hubspot.com/t5/CMS-Development/Dedupe-Contacts-with-Programmable-Automations/m-p/779312#M33696</link>
      <description>&lt;P&gt;Thanks for your suggestion. I did add the error handliong code and it does show the error:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;TypeError: Cannot read properties of undefined (reading 'properties')&lt;/PRE&gt;</description>
      <pubDate>Fri, 07 Apr 2023 13:35:54 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/Dedupe-Contacts-with-Programmable-Automations/m-p/779312#M33696</guid>
      <dc:creator>GKostadinova</dc:creator>
      <dc:date>2023-04-07T13:35:54Z</dc:date>
    </item>
  </channel>
</rss>

