ZIsl94
October 3, 2023, 6:50pm
1
Hey,
I wanted some help editing the template to merge duplicate contacts to delete them instead based on several criteria.
This page is the sample code for merging duplicate contacts (https://github.com/HubSpot/sample-workflow-custom-code/blob/main/samples/dedupe_contact.js ) I would like to edit this so that it delete duplicate contacts instead. Can someone assist me?
Hey, there! Can you share what you’ve already tried? The community can help with your code and errors you are getting. But custom code or code rewriting is beyond what the community can reasonably provide.
If it were me, I’d look to swap out the merge endpoint call with the DELETE (ARCHIVE) endpoint , update your variables to match, and edit the console logs.
hubspotClient.crm.contacts.basicApi.archive(idToDelete)
Have fun building! — Jaycee
ZIsl94
October 5, 2023, 1:22pm
3
Hi Jaycee,
I can share what I have come up with based on the template for merging duplicates. Its been a while since I’ve touched javascript so want to make sure I’m doing it correctly. Your feedback is greatly appreciated!
Thanks,
Z
const hubspot = require(HS API Link);
const HUBSPOT_PRIVATE_APP_ACCESS_TOKEN = process.env.YOUR_PRIVATE_APP_ACCESS_TOKEN;
exports.main = (event, callback) => {
const hubspotClient = new hubspot.Client({ accessToken: HUBSPOT_PRIVATE_APP_ACCESS_TOKEN });
hubspotClient.crm.contacts.basicApi
.getById(event.object.objectId, ['email', 'firstname', 'lastname', 'phone'])
.then(contactResult => {
const email = contactResult.properties.email;
const firstname = contactResult.properties.firstname;
const lastname = contactResult.properties.lastname;
const phone = contactResult.properties.phone;
if (email && firstname && lastname && phone) {
console.log('Contact has values for all properties; skipping deletion.');
return;
}
console.log(`Looking for duplicates based on firstname=${firstname}, lastname=${lastname}`);
hubspotClient.crm.contacts.searchApi.doSearch({
filterGroups: [{
filters: [
{ propertyName: 'firstname', operator: 'EQ', value: firstname },
{ propertyName: 'lastname', operator: 'EQ', value: lastname }
]
}]
})
.then(searchResults => {
const duplicateContacts = searchResults.results.filter(contact => {
const contactEmail = contact.properties.email;
const contactFirstname = contact.properties.firstname;
const contactLastname = contact.properties.lastname;
const contactPhone = contact.properties.phone;
return (
contact.id !== event.object.objectId &&
(!contactEmail || !contactFirstname || !contactLastname || !contactPhone)
);
});
if (duplicateContacts.length === 0) {
console.log('No matching contact, nothing to delete');
return;
} else {
const idsToDelete = duplicateContacts.map(contact => contact.id);
console.log(`Found duplicate contact IDs ${idsToDelete.join(', ')} to delete`);
Promise.all(idsToDelete.map(id => deleteContact(hubspotClient, id)))
.then(() => {
console.log('Duplicate contacts deleted!');
})
.catch(error => {
console.error('Error deleting duplicate contacts:', error);
});
}
});
});
};
function deleteContact(client, contactId) {
return client.crm.contacts.basicApi.deleteById(contactId);
}