CMS Development

Bob2245
Top colaborador(a) | Parceiro Platinum
Top colaborador(a) | Parceiro Platinum

IF statement to check if crm_object query returns no results

resolver

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 Avaliação positiva
1 Solução aceita
Bob2245
Solução
Top colaborador(a) | Parceiro Platinum
Top colaborador(a) | Parceiro Platinum

IF statement to check if crm_object query returns no results

resolver

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 🙂 

Exibir solução no post original

2 Respostas 2
Bob2245
Solução
Top colaborador(a) | Parceiro Platinum
Top colaborador(a) | Parceiro Platinum

IF statement to check if crm_object query returns no results

resolver

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
Equipe de Produto da HubSpot
Equipe de Produto da HubSpot

IF statement to check if crm_object query returns no results

resolver

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