# Custom code to count associated records.

**URL:** <https://community.hubspot.com/t/custom-code-to-count-associated-records/74501>\
**Category:** APIs & Integrations\
**Created:** [February 4, 2023, 11:45pm UTC](https://community.hubspot.com/t/custom-code-to-count-associated-records/74501 "2023-02-04T23:45:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![TH7](https://avatars.discourse-cdn.com/v4/letter/t/6a8cbe/32.png) [@TH7](https://community.hubspot.com/u/TH7)\
**Post date:** [February 4, 2023, 11:45pm UTC](https://community.hubspot.com/t/custom-code-to-count-associated-records/74501/1 "2023-02-04T23:45:07Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Jaycee\_Lewis](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jaycee_lewis/32/61352_2.png) [@Jaycee\_Lewis](https://community.hubspot.com/u/Jaycee_Lewis)\
**Post date:** [February 6, 2023, 5:51pm UTC](https://community.hubspot.com/t/custom-code-to-count-associated-records/74501/2 "2023-02-06T17:51:40Z")

</div>

Hi @TH7 👋 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](https://developers.hubspot.com/docs/api/crm/search)? 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

---

<div class="post-metadata">

**Author:** ![TH7](https://avatars.discourse-cdn.com/v4/letter/t/6a8cbe/32.png) [@TH7](https://community.hubspot.com/u/TH7)\
**Post date:** [February 8, 2023, 2:58am UTC](https://community.hubspot.com/t/custom-code-to-count-associated-records/74501/3 "2023-02-08T02:58:37Z")

</div>

Hi [@Jaycee\_Lewis](https://community.hubspot.com/t5/user/viewprofilepage/user-id/127074) 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!

---

<div class="post-metadata">

**Author:** ![MSchallinger](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/mschallinger/32/64711_2.png) [@MSchallinger](https://community.hubspot.com/u/MSchallinger)\
**Post date:** [August 14, 2024, 5:59pm UTC](https://community.hubspot.com/t/custom-code-to-count-associated-records/74501/4 "2024-08-14T17:59:04Z")

</div>

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.

 ![Power of One Property.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/e/1/e10b37fce9d97c5e331cd6078d27e15c2f40469f.png)

 ![Company Property Counting Number of Associated .png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/9/e/9e4cd73f4deead9ceebf0c124946238fbf6fe973.png)
