Script: Deduplicate Contacts based on firstname, lastname & company name
Hello, I've build the following custom code to deduplicate contacts based on 3 constants: firstname, lastname and company name:
const DEDUPE_PROPERTY1 = 'firstname';
const DEDUPE_PROPERTY2 = 'lastname';
const DEDUPE_PROPERTY3 = 'company';
const hubspot = require('api_key');
exports.main = (event, callback) => {
// Make sure to add your API key under "Secrets" above.
const hubspotClient = new hubspot.Client({
accessToken: process.env.secretName
});
hubspotClient.crm.contacts.basicApi
.getById(event.object.objectId, [DEDUPE_PROPERTY1, DEDUPE_PROPERTY2, DEDUPE_PROPERTY3])
.then(contactResult => {
let dedupePropValue1 = contactResult.body.properties[DEDUPE_PROPERTY1];
let dedupePropValue2 = contactResult.body.properties[DEDUPE_PROPERTY2];
let dedupePropValue3 = contactResult.body.properties[DEDUPE_PROPERTY3];
console.log(`Looking for duplicates based on ${DEDUPE_PROPERTY1} = ${dedupePropValue1}, ${DEDUPE_PROPERTY2} = ${dedupePropValue2}, ${DEDUPE_PROPERTY3} = ${dedupePropValue3}`);
hubspotClient.crm.contacts.searchApi
.doSearch({
filterGroups: [
{
filters: [
{
propertyName: DEDUPE_PROPERTY1,
operator: 'EQ',
value: dedupePropValue1
},
{
propertyName: DEDUPE_PROPERTY2,
operator: 'EQ',
value: dedupePropValue2
},
{
propertyName: DEDUPE_PROPERTY3,
operator: 'EQ',
value: dedupePropValue3
}
]
}
]
})
.then(searchResults => {
let idsToMerge = searchResults.body.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");
}
let idToMerge = idsToMerge[0];
console.log(`Merging enrolled contact id=${event.object.objectId} into contact id=${idToMerge}`);
hubspotClient
.apiRequest({
method: 'POST',
path: `/contacts/v1/contact/merge-vids/${idToMerge}`,
body: {
vidToMerge: event.object.objectId
}
})
.then(mergeResult
But when I test it I get this error:
: The logs for this function have exceeded the 4KB limit.
...
2023-03-10T08:15:38.319Z undefined ERROR Uncaught Exception {"errorType":"Runtime.UserCodeSyntaxError","errorMessage":"SyntaxError: missing ) after argument list","stack":["Runtime.UserCodeSyntaxError: SyntaxError: missing ) after argument list"," at _loadUserApp (file:///var/runtime/index.mjs:993:17)"," at async Object.UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1031:21)"," at async start (file:///var/runtime/index.mjs:1194:23)"," at async file:///var/runtime/index.mjs:1200:1"]}
2023-03-10T08:15:39.800Z undefined ERROR Uncaught Exception {"errorType":"Runtime.UserCodeSyntaxError","errorMessage":"SyntaxError: missing ) after argument list","stack":["Runtime.UserCodeSyntaxError: SyntaxError: missing ) after argument list"," at _loadUserApp (file:///var/runtime/index.mjs:993:17)"," at async Object.UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1031:21)"," at async start (file:///var/runtime/index.mjs:1194:23)"," at async file:///var/runtime/index.mjs:1200:1"]}
START RequestId: 14fda2fd-3ac4-4916-90c7-9a61b0c315ec Version: $LATEST
Unknown application error occurred
Runtime.UserCodeSyntaxError
Memory: 13/128 MB
Runtime: 1679.44 ms
Do you know why? Can you help make sure my script works? I'm not super efficient in js
Script: Deduplicate Contacts based on firstname, lastname & company name
It looks like the error is caused by a missing closing parenthesis ')' somewhere in your code. From the error message, it's hard to tell exactly where the error is occurring. However, I noticed that the code you provided is incomplete - it seems to be missing the end of the .then block where the mergeResult variable is being passed as an argument.
To help you debug the issue, you could try using a code editor or IDE that has syntax highlighting and error checking features. This can make it easier to spot syntax errors like missing parentheses or brackets.
Additionally, I would recommend adding some console.log statements throughout your code to help identify where the error is occurring. You could start by adding a console.log statement at the end of the .then block where the mergeResult variable is being passed as an argument. This can help you confirm that the mergeResult variable is defined and has the expected value.
Here is an example of what the missing code might look like:
Your log is too large. Hubspot has an article about this and suggests this solution.
"If you receive this error: A server error occurred: WARNING: The logs for this function have exceeded the 4KB limit, your log is too large. This can be caused by trying to console log a very large object, or by a lot of separate console logs. To resolve this, reduce how much you're trying to log, hit your endpoint, then run the command again."
Script: Deduplicate Contacts based on firstname, lastname & company name
Hello Eli!
Thank you so much for your reply (sorry for my late reply I just saw this). I'm not very good at coding, would you know how I could implement the solution you suggested in my script?