Custom code to count associated records.

Hello.

Does anyone have experience creating custom calculations that count associated records or return minimum/maximum dates?

For example,

I create a custom object record whenever the recent conversion field changes and associate it with the contact.

I’d like to be able to roll up the count of these records into a custom property field on the contact record.

I am new to coding in Hubspot and know basic Python. Eager to learn and open to any resources y’all may have.

Thank you!

Hi @TH7 :waving_hand: Thanks for reaching out. I have a few questions to help the community better understand your project.

  • Are you wanting to get a count of Custom object records for a Contact?
  • And return that as a value for a custom Contact property?
  • Have you already looked into the Search API? If not, I’d suggest looking at how you can leverage it to query a Contact for its associations, for example.

I’ll also leave this here so other community members can offer some general suggestions.

Best,

Jaycee

Hi @Jaycee_Lewis that’s exactly right.

Just like the company object has “number of associated” contacts, I would like to have “number of associated [custom object records]” in a custom contact property.

Regarding the Search API, could it be used in a custom coded workflow action? I thought I could only reference private apps.

Thank you!

There’s a way to do this without “custom code”.

  1. On the custom object, create a calculated property called “Power of One” (this is how I’ve heard it referenced elsewhere) that’s a custom equation and always equal to “1”.
  2. On the company object, create a calculated property called “Number of Associated [custom object]” that’s a Count equation that counts up all the 1’s of the “Power of One” porperty.