# Cannot merge two contacts using custom code in workflow

**URL:** <https://community.hubspot.com/t/cannot-merge-two-contacts-using-custom-code-in-workflow/130510>\
**Category:** Data Hub\
**Created:** [April 24, 2025, 4:37pm UTC](https://community.hubspot.com/t/cannot-merge-two-contacts-using-custom-code-in-workflow/130510 "2025-04-24T16:37:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![customtruck](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/customtruck/32/29572_2.png) [@customtruck](https://community.hubspot.com/u/customtruck)\
**Post date:** [April 24, 2025, 4:37pm UTC](https://community.hubspot.com/t/cannot-merge-two-contacts-using-custom-code-in-workflow/130510/1 "2025-04-24T16:37:58Z")

</div>

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](https://github.com/HubSpot/sample-workflow-custom-code/blob/main/samples/dedupe_contact.js)  
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).

---

<div class="post-metadata">

**Author:** ![RubenBurdin](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/rubenburdin/32/75212_2.png) [@RubenBurdin](https://community.hubspot.com/u/RubenBurdin)\
**Post date:** [April 24, 2025, 5:31pm UTC](https://community.hubspot.com/t/cannot-merge-two-contacts-using-custom-code-in-workflow/130510/2 "2025-04-24T17:31:59Z")

</div>

Hi @customtruck  
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.  
• Custom-code boilerplate that works:

```lia-code-sample
const hubspot = require('@hubspot/api-client');
exports.main = async (event, cb) => {
 const hs = new hubspot.Client({ accessToken: process.env.HUBSPOT_API_KEY });
 const primaryId = event.inputFields['hs_object_id']; // enrolled contact
 const dupId = event.inputFields['duplicate_id']; // fetched via earlier search
 try {
 await hs.apiRequest({
 method: 'POST',
 path: '/crm/v3/objects/contacts/merge',
 body: { primaryObjectId: primaryId, objectIdToMerge: dupId }
 });
 cb({ outputFields: { merge_status: 'queued' }});
 } catch (e) {
 cb({ outputFields: { merge_status: `error: ${e.message}` }});
 }
};

```

(Replace duplicate\_id with whatever search step you’re using.)  
Once you have the right scopes and exact JSON body, contacts will merge every time—log shows 200, a few seconds later the duplicate VID disappears.  
Hope it helps.

![RubenB_0-1744951380316.png](https://d2qpr2jzcazej7.cloudfront.net/original/2X/a/a3b971f388794f90704ee697c25d92cf1613f49a.png)

**[Ruben Burdin](https://www.linkedin.com/in/ruben-burdin/)**

**Real-Time Data Sync Between any CRM or Database | Founder @[Stacksync](https://l.stacksync.com/hubspot) (YC W24)**

---

<div class="post-metadata">

**Author:** ![customtruck](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/customtruck/32/29572_2.png) [@customtruck](https://community.hubspot.com/u/customtruck)\
**Post date:** [April 25, 2025, 5:15pm UTC](https://community.hubspot.com/t/cannot-merge-two-contacts-using-custom-code-in-workflow/130510/3 "2025-04-25T17:15:42Z")

</div>

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!
