I’ve got futher along by taking a different approach with the property values, since I don’t really see the need to get the contact object and interrogate the properties that way, when they are immediately available to me do to contact enrollment in the workflow.
So I’m pulling them using `event.inputFields`
I then pass the 3 property names and their values as 3 different search filter groups. However, the next 4 lines of code after that throw an undefined error again. Since I can’t tell in this code editor what has actually been returned by the search it’s hard to tell what’s wrong.
/**
* 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 hubspot = require('@hubspot/api-client');
const DEDUPE_PROPERTY1 = 'firstname';
const DEDUPE_PROPERTY2 = 'lastname';
const DEDUPE_PROPERTY3 = 'company';
const PROPERTY_USED_TO_DEDUPE_CONTACTS = 'phone';
exports.main = (event, callback) => {
// Make sure to add your API key under "Secrets" above.
const hubspotClient = new hubspot.Client({
accessToken: process.env.mContact
});
hubspotClient.crm.contacts.basicApi
.getById(event.object.objectId, ['email'])
.then(contactResult => {
const firstNameVal = event.inputFields.firstname;
const lastNameVal = event.inputFields.lastname;
const companyNameVal = event.inputFields.company;
console.log(`Looking for duplicates based on ${DEDUPE_PROPERTY1} = ${firstNameVal} && ${DEDUPE_PROPERTY2} = ${lastNameVal} && ${DEDUPE_PROPERTY3} = ${companyNameVal}`);
hubspotClient.crm.contacts.searchApi
.doSearch({
filterGroups: [{
filters: [
{
propertyName: 'firstname',
operator: 'EQ',
value: firstNameVal
},
{
propertyName: 'lastname',
operator: 'EQ',
value: lastNameVal
},
{
propertyName: 'company',
operator: 'EQ',
value: companyNameVal
}
]
}]
})
.then(searchResults => {
let idsToMerge = searchResults.body.results
.map(object => object.id)
.filter(vid => Number(vid) !== Number(event.object.objectId));
});
});
};
This is the error returned:
2024-04-20T05:27:56.674Z INFO Looking for duplicates based on firstname = Taylor && lastname = Newman && company = Flight Digital
2024-04-20T05:27:56.897Z ERROR Unhandled Promise Rejection {"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"TypeError: Cannot read properties of undefined (reading 'results')","reason":{"errorType":"TypeError","errorMessage":"Cannot read properties of undefined (reading 'results')","stack":["TypeError: Cannot read properties of undefined (reading 'results')"," at /var/task/file.js:53:47"," at processTicksAndRejections (node:internal/process/task_queues:96:5)"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: TypeError: Cannot read properties of undefined (reading 'results')"," at process.<anonymous> (file:///var/runtime/index.mjs:1276:17)"," at process.emit (node:events:513:28)"," at emit (node:internal/process/promises:140:20)"," at processPromiseRejections (node:internal/process/promises:274:27)"," at processTicksAndRejections (node:internal/process/task_queues:97:32)"]}
Unknown application error occurred
Runtime.Unknown
Those filter settings should be returning two contacts in the results: