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:
/** * 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) */
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})`);
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).
This might help you • Use the POST /crm/v3/objects/contacts/merge endpoint (not the old “merge-two” v1 URL). Body must be: { "primaryObjectId": "123", "objectIdToMerge": "456" }—nothing else, no JSON array. • Your private-app token needs crm.objects.contacts.write plus crm.objects.contacts.read; without write, HubSpot returns 200 in custom-code but silently drops the merge. • v3 runs the merge async, so the 200 you log only means “request accepted.” It can take a few seconds—add a short delay or a polling step if you need to confirm inside the same workflow run. • If you’re matching on mobilephone, be sure both records have an identical value in that exact internal property; HubSpot won’t merge on a partial match or on other phone fields.
This might help you • Use the POST /crm/v3/objects/contacts/merge endpoint (not the old “merge-two” v1 URL). Body must be: { "primaryObjectId": "123", "objectIdToMerge": "456" }—nothing else, no JSON array. • Your private-app token needs crm.objects.contacts.write plus crm.objects.contacts.read; without write, HubSpot returns 200 in custom-code but silently drops the merge. • v3 runs the merge async, so the 200 you log only means “request accepted.” It can take a few seconds—add a short delay or a polling step if you need to confirm inside the same workflow run. • If you’re matching on mobilephone, be sure both records have an identical value in that exact internal property; HubSpot won’t merge on a partial match or on other phone fields.
Cannot merge two contacts using custom code in workflow
SOLVE
Wow, I've been beating my head against this wall for hours and all it came down to was using "objectIdToMerge" instead of "secondaryObjectId", as you suggested. I am so relieved to finally have this working -- thank you so much for your timely assistance!