We team I am write custom code to marge dublicate vai API but i got error while there contact have >2 contact as same conatct in my hubspot I am using this API
Can you please provide me the solution to marge more than 2 conatct via API
The question is a bit hard to answer without an example of your code, however;
This can not be achieved with a single call, so you would first have to merge the first two contacts, and then merge the last one in an additional API call. This should be achievable with a small for loop in your code.
Learn more about HubSpot by following me on LinkedIn or YouTube
✅ Did my answer solve your issue? Help the community by marking it as the solution.
The question is a bit hard to answer without an example of your code, however;
This can not be achieved with a single call, so you would first have to merge the first two contacts, and then merge the last one in an additional API call. This should be achievable with a small for loop in your code.
Learn more about HubSpot by following me on LinkedIn or YouTube
✅ Did my answer solve your issue? Help the community by marking it as the solution.
let dedupePropValue = contactResult.properties[PROPERTY_USED_TO_DEDUPE_CONTACTS];
if (!dedupePropValue) { console.log(`No value found for ${PROPERTY_USED_TO_DEDUPE_CONTACTS}`); return; } console.log("Phone number:", dedupePropValue);
// Ensure the dedupe property is not empty or invalid if (!dedupePropValue) { console.log(`No value found for ${PROPERTY_USED_TO_DEDUPE_CONTACTS}, skipping deduplication.`); return; } console.log(`Looking for duplicates based on ${PROPERTY_USED_TO_DEDUPE_CONTACTS} = ${dedupePropValue}`); // Search for duplicate contacts const searchResults = await hubspotClient.crm.contacts.searchApi.doSearch({ filterGroups: [{ filters: [{ propertyName: PROPERTY_USED_TO_DEDUPE_CONTACTS, operator: 'EQ', value: dedupePropValue }] }] }); // Collect IDs of contacts that match the dedupe property but aren't the current contact let idsToMerge = searchResults.results .map(object => object.id) .filter(vid => Number(vid) !== Number(event.object.objectId));
if (idsToMerge.length == 0) { console.log('No matching contact, nothing to merge.'); return; } else if (idsToMerge.length > 1) { console.log(`Found multiple potential contact IDs ${idsToMerge.join(', ')} to merge`); throw new Error("Ambiguous merge; more than one matching contact"); } // Merge the contacts let idToMerge = idsToMerge[0]; console.log(`Merging enrolled contact id=${event.object.objectId} into contact id=${idToMerge}`); const mergeResult = await hubspotClient.apiRequest({ method: 'POST', path: `/contacts/v1/contact/merge-vids/${idToMerge}`, body: { vidToMerge: event.object.objectId } }); console.log('Contacts merged successfully!'); } catch (error) { console.error('Error during contact deduplication:', error.message); } };