I would like to have a property that counts the number of calls made against a specific contact - I know that this isn't possible with an out the box solution.
Is there a way to use Custom Code to count the activities against a contact upon a specific trigger?
You can filter based on specific criteria that you are looking at for each call you want to count. ie, Call Status is completed, or a specific time frame you would like to look at, or specific users you want to track.
You can then extract the 'total' value from the api response which is a count of all the calls that match your search.
Here's a little sample of how I've done it in the past:
const hubspot = require('@hubspot/api-client');
exports.main = async (event, callback) => {
const hubspotClient = new hubspot.Client({"accessToken":process.env.YOUR_SECRET});
var id = event.inputFields['id'];
const PublicObjectSearchRequest = {
filterGroups: [
{
"filters":[
{"propertyName":"associations.contact",
"operator":"EQ",
"value": id
}
// ADD ANY OTHER FILTERS YOU WOULD LIKE TO USE HERE. MAX OF 3 PER FILTER GROUP.
]
}
],
properties:[]
};
var count_of_calls;
try {
const apiResponse = await hubspotClient.crm.objects.emails.searchApi.doSearch(PublicObjectSearchRequest);
count_of_calls = apiResponse.total
} catch (e) {
console.log(e);
throw e
}
callback({
outputFields: {
count_of_calls: count_of_calls
}
});
}
You will obviously need to add your own secret key into this code, and add the contact record id as an input field.
You can filter based on specific criteria that you are looking at for each call you want to count. ie, Call Status is completed, or a specific time frame you would like to look at, or specific users you want to track.
You can then extract the 'total' value from the api response which is a count of all the calls that match your search.
Here's a little sample of how I've done it in the past:
const hubspot = require('@hubspot/api-client');
exports.main = async (event, callback) => {
const hubspotClient = new hubspot.Client({"accessToken":process.env.YOUR_SECRET});
var id = event.inputFields['id'];
const PublicObjectSearchRequest = {
filterGroups: [
{
"filters":[
{"propertyName":"associations.contact",
"operator":"EQ",
"value": id
}
// ADD ANY OTHER FILTERS YOU WOULD LIKE TO USE HERE. MAX OF 3 PER FILTER GROUP.
]
}
],
properties:[]
};
var count_of_calls;
try {
const apiResponse = await hubspotClient.crm.objects.emails.searchApi.doSearch(PublicObjectSearchRequest);
count_of_calls = apiResponse.total
} catch (e) {
console.log(e);
throw e
}
callback({
outputFields: {
count_of_calls: count_of_calls
}
});
}
You will obviously need to add your own secret key into this code, and add the contact record id as an input field.
Before being able to come up with solutions, could you please help me understand why you need to count the number of calls per contact? Would you have automation triggered on the back of that property or would you need it for reporting, etc? As if it's for reporting, in the custom report builder the "Count of calls" is already available as a metric. In the test report, I made the system show me the number of calls per email, which would be per contact and the report showed me the real data.
When I know more about the use case, I can see what else is possible. 🙂
It is for reporting so this is a great help, thank you - the reason why I wanted to do this via a workflow and properties is so that then I could a) filter out which calls/activities increase the count (so I can see sales calls vs success calls per account) but then also use calculation properties to aggregate that to a company level easily 🙂
Thank you for reaching out and for your question. I found a similar idea submittedhere. I would recommend upvoting and commenting on your use case, so you're automatically subscribed to the idea.
You'll receive notifications when our product team makes a status update to the idea.
Thank you,
Diana
HubSpot’s AI-powered customer agent resolves up to 50% of customer queries instantly, with some customers reaching up to 90% resolution rates. Learn More.
HubSpot’s AI-powered customer agent resolves up to 50% of customer queries instantly, with some customers reaching up to 90% resolution rates. Learn More.