CMS Development

Bob2245
Top Contributor | Platinum Partner
Top Contributor | Platinum Partner

IF statement to check if crm_object query returns no results

SOLVE

So, we've built a programmatic email template for a client that queries a custom object for records matching certain criteria, then returns a bunch of fields of those records to be displayed in the email. 

 

These custom object records are fed to Hubspot on a daily basis from an external system. There's days when the external system has no new records to feed, so the email would be sent with a blank section. So I'd like to use an IF statement to display fallback content in case no records were found. Here's the snippet of code where this happens. Piece in the red block is what I added to make this work, but it didn't seem to work. 

 

Bob2245_0-1637764026026.png

I understood something like this should be possible, but I'm guessing this isn't it. 

0 Upvotes
1 Accepted solution
Bob2245
Solution
Top Contributor | Platinum Partner
Top Contributor | Platinum Partner

IF statement to check if crm_object query returns no results

SOLVE

Oh, figured it out. That suggestion made total sense, but seems like the crm_objects never returns a complete null result, even if there's no records that match the query. So it would always just render a white space in my email. 

 

Had another look at the documentation, and crm_objects returns a total parameter. So I just added:

 

{% if events.total == "0" %}
Some test text here

{% else %}
The for loop

{% endif %}

 

Then purposefully set my query to mess up to test results, and it worked 🙂 

View solution in original post

2 Replies 2
Bob2245
Solution
Top Contributor | Platinum Partner
Top Contributor | Platinum Partner

IF statement to check if crm_object query returns no results

SOLVE

Oh, figured it out. That suggestion made total sense, but seems like the crm_objects never returns a complete null result, even if there's no records that match the query. So it would always just render a white space in my email. 

 

Had another look at the documentation, and crm_objects returns a total parameter. So I just added:

 

{% if events.total == "0" %}
Some test text here

{% else %}
The for loop

{% endif %}

 

Then purposefully set my query to mess up to test results, and it worked 🙂 

dennisedson
HubSpot Product Team
HubSpot Product Team

IF statement to check if crm_object query returns no results

SOLVE

@Bob2245 

You can remove the == null and put your for loop ablove the else.  Then below the else, add your alternative message.

EG:

{% if events %}
  this will occur if there is a value for events
{% else %}
  this will occur if there is NOT a value for events
{% endif %}