IF statement to check if crm_object query returns no results

SOLVE
Bob2245
Contributor | Diamond Partner

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

Accepted Solutions
Bob2245
Solution
Contributor | Diamond Partner

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
dennisedson
Community Manager

@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 %}

 

Thanks,

Dennis




Check out our Community Developer Blog
where we feature our Community driven developer podcast and how to content
Bob2245
Solution
Contributor | Diamond Partner

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