I am attempting to use the custom code workflow capabilities in Operation Hub to merge two contacts if they match on the mobilephone (internal name) property. This is very similar to the use case provided by Hubspot staff here sample-workflow-custom-code/samples/dedupe_contact.js at main · HubSpot/sample-workflow-custom-code · GitHub
However, that code does nothing anymore when run and gives linting tips that mergeResults is defined but never used, and uses the now deprecated v1 APIs.
I’ve tried rewriting it to use the v3 merge API and the log does show that the workflow successfully found a matching contact ID with the same mobilephone number as the enrolled contact’s, and it says it successfully sent the merge request to the merge API for those two IDs. But they are not merged. Any idea why? Here’s the current node.js code:
Spoiler
/**
* HubSpot Workflow Custom Code: Merge contacts by matching mobilephone
*
* Logic:
* - If NO other contact matches → do nothing
* - If EXACTLY 1 other contact matches → MERGE enrolled contact into that contact
* - If 2+ other contacts match → skip merge (ambiguous)
*/
const hubspot = require(‘@hubspot/api-client’);
const DEDUPE_PROPERTY = ‘mobilephone’;
exports.main = async (event, callback) => {
const hubspotClient = new hubspot.Client({
accessToken: process.env.[SECRET]
});
try {
const enrolledId = event.object.objectId;
const enrolledIdStr = String(enrolledId);
// 1) Fetch enrolled contact’s mobilephone
const contact = await hubspotClient.crm.contacts.basicApi.getById(
enrolledId,
[DEDUPE_PROPERTY]
);
const phoneValue = contact.properties?.[DEDUPE_PROPERTY];
if (!phoneValue) {
console.log(‘No mobilephone on enrolled contact; nothing to merge.’);
return callback(null, { status: ‘no_mobilephone’ });
}
console.log(`Searching for contacts with ${DEDUPE_PROPERTY} = ${phoneValue}`);
// 2) Search for contacts with matching mobilephone
const search = await hubspotClient.crm.contacts.searchApi.doSearch({
filterGroups: [{
filters: [{
propertyName: DEDUPE_PROPERTY,
operator: ‘EQ’,
value: phoneValue
}]
}],
limit: 10
});
const matchingIds = (search.results || [])
.map(c => c.id)
.filter(id => String(id) !== enrolledIdStr);
console.log(`Found ${matchingIds.length} other contacts with same mobilephone.`);
// --- Merge Logic ---
if (matchingIds.length === 0) {
console.log(‘No duplicates found. Nothing to merge.’);
return callback(null, { status: ‘no_duplicates’ });
}
if (matchingIds.length >= 2) {
console.log(‘Ambiguous: 2 or more other contacts found. No merge performed.’);
return callback(null, { status: ‘ambiguous_duplicates’ });
}
// 3) Exactly 1 match → perform merge using direct API call
const primaryId = matchingIds[0];
console.log(`Merging enrolled contact (${enrolledId}) INTO contact (${primaryId})`);
try {
await hubspotClient.apiRequest({
method: ‘POST’,
path: ‘/crm/v3/objects/contacts/merge’,
body: {
primaryObjectId: primaryId,
secondaryObjectId: enrolledId
}
});
console.log(‘
Merge request sent successfully (HubSpot may return 204 No Content).’);
return callback(null, { status: ‘merged’, merged_into: primaryId });
} catch (mergeErr) {
console.error(‘
Merge API failed:’, mergeErr.response?.body || mergeErr);
return callback(mergeErr);
}
} catch (err) {
console.error(‘
General error during merge process:’, err);
return callback(err);
}
};
And here’s the latest log:
Spoiler
Logs
2025-04-24T16:21:28.815Z INFO Searching for contacts with mobilephone = [hidden] 2025-04-24T16:21:29.135Z INFO Found 1 other contacts with same mobilephone. 2025-04-24T16:21:29.136Z INFO Merging enrolled contact (12479851) INTO contact (44946951) 2025-04-24T16:21:29.258Z INFO
Merge request sent successfully (HubSpot may return 204 No Content).
