CMS Development

KimM
Top Contributor

Issue with looping through multiple layers of CRM Associations

SOLVE

Hi Hubspot Community,

 

I'm at a bit of a loss here and can't seem to find the solution to a problem I'm having. I'm essentially trying to create a client events calendar page that would be hosted within a client hub (password protected). This page essentially pulled in CRM objects (deals) based on their association with the company selected on a module custom field. I have all of this working fine.

 

However, I want to also be able to then use the first associated contact (for the deal not the company) to give the user the ability to filter through the results. However, when I try and loop through the contacts I only get 9 results when there should be 24.

 

Here's a snippet of what I've been doing:

{% set associated_deals = crm_associations( module.company.id, 'HUBSPOT_DEFINED', 6, 'limit=100&orderBy=event_date', 'hs_object_id') %} 
{% for deal in associated_deals.results %}
{% set associated_contacts = crm_associations(deal.hs_object_id, 'HUBSPOT_DEFINED', 3, 'limit=1', 'firstname,lastname,hs_object_id') %}
    {% for contact in associated_contacts.results %}
      <p>{{ contact.firstname }} {{ contact.lastname }}: {{contact.hs_object_id}}<br></p>
    {% endfor %}
  {% endfor %}

I can't seem to find any documentation about this kind of issue and not sure exactly what I'm doing wrong. Any guidance or help would be greatly appreciated.

0 Upvotes
1 Accepted solution
KimM
Solution
Top Contributor

Issue with looping through multiple layers of CRM Associations

SOLVE

Alright, I've found a solution. This is probably not the best way to handle this but it at least works for our use case. I'll share how I went about it below in case anyone else wants to refer back to this in future.

 

What I'm trying to achieve:

A table with all the deals associated with a company, and then be able to filter them by their associated contact. 

 

The problem/s:

You can only make 10 Hubspot CRM Object requests per page according to Hubspot support, so looping through each deal to find the associated contact won't work.

 

My solution:

Instead of looping through these calls, I decided to make two calls; one pulling all the associated deals with a company and one pulling all the associated contacts.

I used workflows to map the Contact ID to it's associated deal with a custom property (Note this only really works where you have one associated contact to a deal. When there is multiple you would need Operations Hub and some tweaking to the code below) and then used this property to associated a deal with a contact.

 

Here's basically what it looks like:

// Pull Associated Deals

{% set associated_deals = crm_associations( module.company.id, 'HUBSPOT_DEFINED', 6, 'limit=100&orderBy=event_date', 'dealname,event_date,hs_object_id,associated_contact_id') %} 

// Create list to append contact ID's to
{% set contactidlist = [] %}

// Append to Contact ID List a list of unique contact ID's
{% for deal in associated_deals.results %}
  {% if contactidlist is not containing deal.associated_contact_id %}
    {% do contactidlist.append(deal.associated_contact_id) %}
  {% endif %}
{% endfor %}

// Request all Contacts Associated with Company
{% set all_contacts = crm_associations( module.company.id, 'HUBSPOT_DEFINED', 2, 'limit=260&orderBy=firstname', 'firstname, lastname, hs_object_id') %} 

// Compare the Contacts results with contactidlist and only action results that match
{% for contact in all_contacts.results %}
  {% if contactidlist is containing contact.hs_object_id %}
    // Do stuff with contact records
  {% endif %}
{% endfor %}

 

Obviously, there are some key limitations here, the main being it won't handle multiple contacts being associated with a deal through workflows, instead it will likely just pull the most recently associated contact. For me, this is not really a big issue as for 99% of cases there is only one contact associated.

 

If anyone has any suggestions on a better way to do this let me know but hopefully this is helpful to others.

 

Thanks,

Kim

 

View solution in original post

0 Upvotes
4 Replies 4
KimM
Solution
Top Contributor

Issue with looping through multiple layers of CRM Associations

SOLVE

Alright, I've found a solution. This is probably not the best way to handle this but it at least works for our use case. I'll share how I went about it below in case anyone else wants to refer back to this in future.

 

What I'm trying to achieve:

A table with all the deals associated with a company, and then be able to filter them by their associated contact. 

 

The problem/s:

You can only make 10 Hubspot CRM Object requests per page according to Hubspot support, so looping through each deal to find the associated contact won't work.

 

My solution:

Instead of looping through these calls, I decided to make two calls; one pulling all the associated deals with a company and one pulling all the associated contacts.

I used workflows to map the Contact ID to it's associated deal with a custom property (Note this only really works where you have one associated contact to a deal. When there is multiple you would need Operations Hub and some tweaking to the code below) and then used this property to associated a deal with a contact.

 

Here's basically what it looks like:

// Pull Associated Deals

{% set associated_deals = crm_associations( module.company.id, 'HUBSPOT_DEFINED', 6, 'limit=100&orderBy=event_date', 'dealname,event_date,hs_object_id,associated_contact_id') %} 

// Create list to append contact ID's to
{% set contactidlist = [] %}

// Append to Contact ID List a list of unique contact ID's
{% for deal in associated_deals.results %}
  {% if contactidlist is not containing deal.associated_contact_id %}
    {% do contactidlist.append(deal.associated_contact_id) %}
  {% endif %}
{% endfor %}

// Request all Contacts Associated with Company
{% set all_contacts = crm_associations( module.company.id, 'HUBSPOT_DEFINED', 2, 'limit=260&orderBy=firstname', 'firstname, lastname, hs_object_id') %} 

// Compare the Contacts results with contactidlist and only action results that match
{% for contact in all_contacts.results %}
  {% if contactidlist is containing contact.hs_object_id %}
    // Do stuff with contact records
  {% endif %}
{% endfor %}

 

Obviously, there are some key limitations here, the main being it won't handle multiple contacts being associated with a deal through workflows, instead it will likely just pull the most recently associated contact. For me, this is not really a big issue as for 99% of cases there is only one contact associated.

 

If anyone has any suggestions on a better way to do this let me know but hopefully this is helpful to others.

 

Thanks,

Kim

 

0 Upvotes
KimM
Top Contributor

Issue with looping through multiple layers of CRM Associations

SOLVE

Another update on this, I believe that this might have to do with a limit on the number of calls you can make to CRM objects. Currently, only 9 results are being shown as there is first a call to find all of the deals associated with a company.

 

Without this first call, it allows 10 results. Is there a better way to handle these that I'm not aware of?

0 Upvotes
Teun
Recognized Expert | Diamond Partner
Recognized Expert | Diamond Partner

Issue with looping through multiple layers of CRM Associations

SOLVE

Hi @KimM ,

 

As your limit is set to '1' when you retrieve the associated contacts, I assume that there are 24 deals that all have at least one associated contact right?

Are you 100% sure that all deals have a contact associated?



Learn more about HubSpot by following me on LinkedIn or YouTube

Did my answer solve your issue? Help the community by marking it as the solution.


0 Upvotes
KimM
Top Contributor

Issue with looping through multiple layers of CRM Associations

SOLVE

HI Teun,

 

Yes I'm 100% sure they all have contacts associated. Pretty much all of our deals only have one contact associated and I only want this primary contact (the person who submitted the enquiry).

 

I have also tested with increasing the limit but the results are exactly the same.

 

Thanks,

Kim

0 Upvotes