# Dedupe Contacts with Programmable Automations

**URL:** <https://community.hubspot.com/t/dedupe-contacts-with-programmable-automations/77482>\
**Category:** CMS Development\
**Tags:** javascript\
**Created:** [March 19, 2023, 3:28am UTC](https://community.hubspot.com/t/dedupe-contacts-with-programmable-automations/77482 "2023-03-19T03:28:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![GKostadinova](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/gkostadinova/32/41874_2.png) [@GKostadinova](https://community.hubspot.com/u/GKostadinova)\
**Post date:** [March 19, 2023, 3:28am UTC](https://community.hubspot.com/t/dedupe-contacts-with-programmable-automations/77482/1 "2023-03-19T03:28:32Z")

</div>

Hi,  
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’)”

```lia-code-sample
const DEDUPE_PROPERTY = 'firstname';
const hubspot = require('@hubspot/api-client');

exports.main = (event, callback) => {
 // 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 => {
 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 => {
 let idsToMerge = searchResults.body.results
 .map(object => object.id)
 .filter(vid => Number(vid) !== Number(event.object.objectId));

 if (idsToMerge.length == 0) {
 console.log('No matching contact, nothing to merge');
 return;
 } else if (idsToMerge.length > 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 => {
 console.log('Contacts merged!');
 });
 });
 });
};

```

---

<div class="post-metadata">

**Author:** ![himanshurauthan](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/himanshurauthan/32/53076_2.png) [@himanshurauthan](https://community.hubspot.com/u/himanshurauthan)\
**Post date:** [March 20, 2023, 8:10am UTC](https://community.hubspot.com/t/dedupe-contacts-with-programmable-automations/77482/2 "2023-03-20T08:10:57Z")

</div>

Hello @GKostadinova

The error you’re seeing suggests that the `contactResult.body` object is undefined, which means that there is no `properties` property to read from it. This could happen if the `contactResult` object is not properly formed, or if the API call to `hubspotClient.crm.contacts.basicApi.getById` is failing for some reason.

One possible solution is to add error handling to your code to catch any errors that occur during the API calls.  
You can do this by adding a `.catch()` block to each of your API calls, like this:

```lia-code-sample
hubspotClient.crm.contacts.basicApi
 .getById(event.object.objectId, [DEDUPE_PROPERTY])
 .then(contactResult => {
 // ...
 })
 .catch(err => {
 console.error('Error getting contact by ID:', err);
 callback(err);
 });

```

This will log any errors that occur to the console and pass the error back to the calling function using the `callback` function.

You can add similar error handling to the other API calls in your code to make it more robust and easier to debug.

---

<div class="post-metadata">

**Author:** ![GKostadinova](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/gkostadinova/32/41874_2.png) [@GKostadinova](https://community.hubspot.com/u/GKostadinova)\
**Post date:** [April 7, 2023, 1:35pm UTC](https://community.hubspot.com/t/dedupe-contacts-with-programmable-automations/77482/3 "2023-04-07T13:35:54Z")

</div>

Thanks for your suggestion. I did add the error handliong code and it does show the error:

`TypeError: Cannot read properties of undefined (reading 'properties')`
