APIs & Integrations

AGupta21
Member

Duplicate contact if the contact grater than 3

SOLVE

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

0 Upvotes
1 Accepted solution
Teun
Solution
Recognized Expert | Elite Partner
Recognized Expert | Elite Partner

Duplicate contact if the contact grater than 3

SOLVE

Hi @AGupta21 ,


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.


View solution in original post

0 Upvotes
2 Replies 2
Teun
Solution
Recognized Expert | Elite Partner
Recognized Expert | Elite Partner

Duplicate contact if the contact grater than 3

SOLVE

Hi @AGupta21 ,


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.


0 Upvotes
AGupta21
Member

Duplicate contact if the contact grater than 3

SOLVE

@Teun 

Example is if in my Contact i same conatct with more than 3 conatct available i am going to marge  all cotact with single contact .

I am in using this code but when the conatct is more 2 then this code throwing error.

bellow the code here

 

const hubspot = require('@hubspot/api-client');

const PROPERTY_USED_TO_DEDUPE_CONTACTS = 'phone';

exports.main = async (event, callback) => {
const hubspotClient = new hubspot.Client({ accessToken: '' });

try {
console.log("Phone number:", PROPERTY_USED_TO_DEDUPE_CONTACTS);

const contactResult = await hubspotClient.crm.contacts.basicApi.getById(event.object.objectId, [PROPERTY_USED_TO_DEDUPE_CONTACTS]);

console.log("All contact properties:", contactResult.properties);

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);
}
};

0 Upvotes