CMS Development

tcxen
Contributor | Platinum Partner
Contributor | Platinum Partner

Error when using custom code workflow action to merge contacts based on matching phone number

SOLVE

Hi there,

 

We are using a custom code workflow action to attempt to merge contacts if they have a matching Phone Number (phone) contact property.

 

Adapted the code from this example: https://github.com/HubSpot/sample-workflow-custom-code/blob/main/samples/dedupe_contact.js

 

I have a Private App set up as a secret, the Private App has the correct scope.

 

I'm getting this error: 

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

 

The issue it is talking about is on line 21 of that github code sample.

 

Any ideas what I've set up incorrectly?

0 Upvotes
1 Accepted solution
MatthewShepherd
Solution
Key Advisor

Error when using custom code workflow action to merge contacts based on matching phone number

SOLVE

Hi @tcxen

I recently used this code and had to remove the body node from contactResult.body.properties and searchResults.body.results as mentioned in this issue. So they become contactResult.properties and searchResults.results.

Hope that helps.

 

Matthew Shepherd

Freelance HubSpot Consultant
CRM Consultant | SEO Specialist

Did my post help answer your query?Help the community by marking it as a solution.

View solution in original post

5 Replies 5
MatthewShepherd
Solution
Key Advisor

Error when using custom code workflow action to merge contacts based on matching phone number

SOLVE

Hi @tcxen

I recently used this code and had to remove the body node from contactResult.body.properties and searchResults.body.results as mentioned in this issue. So they become contactResult.properties and searchResults.results.

Hope that helps.

 

Matthew Shepherd

Freelance HubSpot Consultant
CRM Consultant | SEO Specialist

Did my post help answer your query?Help the community by marking it as a solution.
Jaycee_Lewis
Community Manager
Community Manager

Error when using custom code workflow action to merge contacts based on matching phone number

SOLVE

Thank you, @MatthewShepherd 🙌

linkedin

Jaycee Lewis

Developer Community Manager

Community | HubSpot

tcxen
Contributor | Platinum Partner
Contributor | Platinum Partner

Error when using custom code workflow action to merge contacts based on matching phone number

SOLVE

Thanks Matthew!

That was it. Now working 🙂

Thanks for replying.

Jaycee_Lewis
Community Manager
Community Manager

Error when using custom code workflow action to merge contacts based on matching phone number

SOLVE

Hey, @tcxen 👋 Thanks for your question! Did you get this worked out?

 

Can you share your code example here, please? Minus your Private App details 😊

 

The code block tool can help with formatting as needed.

 

Seeing how your code looks, helps our community members to give targeted advice. (You didn't do anything wrong, I want to increase our surface area for getting an answer for you if possible)

 

Thanks! — Jaycee

linkedin

Jaycee Lewis

Developer Community Manager

Community | HubSpot

0 Upvotes
tcxen
Contributor | Platinum Partner
Contributor | Platinum Partner

Error when using custom code workflow action to merge contacts based on matching phone number

SOLVE

Thanks @Jaycee_Lewis 

 

Here's my code:


/**
* Searches for another contact with the same value of DEDUPE_PROPERTY.
* - If no matches are found, nothing happens
* - If one match is found, the enrolled contact is merged into the matching contact
* - If more than one match is found, the action fails
*/

const DEDUPE_PROPERTY = 'phone';

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.DedupePrivateApp
});

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 > 1) {
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!');
});
});
});
};

 

 I'm using:

  • Private App set up as a Secret called DedupePrivateApp
  • Node.js 16.x
  • HubSpot Client v8
  • Property to include in code: Phone number property 'phone' 
0 Upvotes