Show contact information on a COS page

Highlighted
New Contributor

Hi there,

 

I just discoverd that I can use the crm_object to get CRM data into a COS page.

 

Eventually, I would like to create a page with a list of contacts who have a certain contact property. I followed the steps from the Hubspot tutorial to produce this code:

 

{% set contact = crm_object("contact", "company=companyname", "firstname,lastname", false) %}

 

{{ contact.firstname }}

{{ contact.lastname }}

 

This returns multiple names. Can somebody help me figure out how to

 

1. Set an argument which pulls out contacts that have a certain custom contact property

2. Loop through the returned 'contact' object in HUBL to display the names of these contacts in a page sequentially

 

Cheers!

Reply
0 Upvotes
5 Replies 5
Highlighted
HubSpot Moderator

Hi, 

 

I would recommend with something like the following

 

{% set contacts = crm_object("contact", "company=companyname", "firstname,lastname", false) %}

{% for contact in contacts %}
  {{ contact.firstname }}
{% endfor %}

This will enable you to create a list of all of the contacts. Once you have the list set you can setup your for loop to get a single contact and then print out the firstname of property that is located on the above contact. 

 

You may also need to use the sort filter located here.  You would only need the sort filter if you need to sort alphabetically for example. 

Highlighted
New Contributor

Thanks for your reply. I tried your suggestion, but the loop doesn't return anything. (looking at the preview of the custom module).

 

When I use:

 

{% set contact = crm_object("contact", "company=companyname", "firstname,lastname", false) %}

{{ contact.firstname }}

 

I see different names being replaced in the preview window, ending with the display of the last name found. But when I use the loop

 

{% set contacts = crm_object("contact", "company=companyname", "firstname,lastname", false) %}

{% for contact in contacts %}
{{ contact.firstname }}
{% endfor %}

 

it doesn't return anything. 

 

Am I missing something here?

Reply
0 Upvotes
Highlighted
HubSpot Moderator

Hi, 

 

I took a look at our documentation a bit more closely and I you're right as we shouldn't need a for loop here to access these properties. I think what is happening here is the query doesn't match any properties in HubSpot and therefore it returns nothing. 

 

This HubL filter should be querying a specific contact or contacts that meet specific argumnets passed to the function. The main problem seems to be the second param you're passing in for companyname. While this is a valid filter you will need to pass in a specific company name to return these values. 

 

For example I created this variable for HubSpot contacts. I pass the company name of hubspot to the companyname param in the second argument and this will return to me any contacts that there company name is set to HubSpot on the contact record. 

 

{% set hubspot_contact = crm_object("contact", "company=hubspot", "firstname,lastname", false) %}
{{ hubspot_contact.firstname }}

I would try passing a specific company name to the second parameter and see if that returns the contact record for you. 

Reply
0 Upvotes
Highlighted
New Contributor

Hi there,

 

I understand that I need a valid company name to pass on to the filter, I changed the name to 'companyname' here for confidentiality purposes. I know I have a correct name which should return me several contacts.

 

The problem seems to be in the loop function that retrieves the names. If I use the filter without the loop, I can see the names appear in the preview window. But while viewing the names seem to replace themselves. If I use the for loop, nothing is returned. 

 

There is also no error. Is there any way to debug this filter?

Reply
0 Upvotes
Highlighted
HubSpot Moderator

Hi, 

 

With the crm_object this function is only going to return a single instance of a contact. I was wrong in using the for loop before too as it does not return a list. What might help to debug this is I use the |pprint filter. When the crm_object a Map is returned normally for author content objects such as blog topics they return a list. The lists are iterable are you can use a for loop to get a single instance of the crm_object. 

 

While this will pull contact information since it only returns a single instance I don't believe this is what you want to use as this will only return one contact. 

 

I hope this helped to clear things up

Reply
0 Upvotes