⚙ Operations Hub

RPearson3
Participant

Custom Code to Count Activities

SOLVE

Hello!

 

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?

 

Thanks

0 Upvotes
1 Accepted solution
KimM
Solution
Top Contributor

Custom Code to Count Activities

SOLVE

I would suggest doing this using the Hubspot engagements calls API utilising the search endpoint: https://developers.hubspot.com/docs/api/crm/calls

 

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.

 

Hope this helps!

 

View solution in original post

7 Replies 7
KimM
Solution
Top Contributor

Custom Code to Count Activities

SOLVE

I would suggest doing this using the Hubspot engagements calls API utilising the search endpoint: https://developers.hubspot.com/docs/api/crm/calls

 

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.

 

Hope this helps!

 

RPearson3
Participant

Custom Code to Count Activities

SOLVE

This looks perfect - thank you!

0 Upvotes
elizheleva
Guide | Gold Partner
Guide | Gold Partner

Custom Code to Count Activities

SOLVE

Hi, @RPearson3 ,@DianaGomez thank you for the tag.

 

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. 🙂

 

elizheleva_0-1683976370785.png

 

Thanks,

Eli


Eli Zheleva
Chief Problem solver @ uspeh | Hubspot Trainer | Hubspot Partner | Community Champion
Still need help? Book a free call with me

Did my post help answer your query? Help the community by marking it as a solution.



RPearson3
Participant

Custom Code to Count Activities

SOLVE

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 🙂 

0 Upvotes
DianaGomez
Community Manager
Community Manager

Custom Code to Count Activities

SOLVE

Hi @RPearson3  I hope you are doing well.

 

Thank you for reaching out and for your question. I found a similar idea submitted here. 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.


¿Sabías que la Comunidad está disponible en Español?
¡Participa hoy en conversaciones en el idioma de tu preferencia,cambiando el idioma en tus configuarciones!
RPearson3
Participant

Custom Code to Count Activities

SOLVE

Does that mean there isn't a way to do it with Custom Code? I know you can't via a standard property.

0 Upvotes
DianaGomez
Community Manager
Community Manager

Custom Code to Count Activities

SOLVE

Hi @RPearson3, thanks for the follow-up question.

 

I wanted to tag in a couple of subject matter experts to see if they have any input on this matter:
hi @elizheleva, @Phil_Vallender, @Mike_Eastwood  do you have any advice for @RPearson3 ? Thank you!

 

Best,
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.


¿Sabías que la Comunidad está disponible en Español?
¡Participa hoy en conversaciones en el idioma de tu preferencia,cambiando el idioma en tus configuarciones!