CMS Development

MNick2
Participant

Check if contact property is empty in email templates

SOLVE

Hello, I am trying to figure out how to check if a contact property is empty via hubl we have so far

 

 

{% if contact.added_to_our_list_after is not defined or contact.added_to_our_list_after == '' or contact.added_to_our_list_after is none %}
<!-- Do something if the property is empty, not defined, or null -->
<p>The contact has not been added to our list after a specific date.</p>
{% else %}
<!-- Do something if the property is not empty -->
<p>The contact was added to our list after: {{ contact.added_to_our_list_after }}</p>
{% endif %}

 


But it doesn't work, it always goes into the else statement, so something I am clearly missing with the value of contact.added_to_our_list_after when blank

0 Upvotes
1 Accepted solution
Kevin-C
Solution
Recognized Expert | Partner
Recognized Expert | Partner

Check if contact property is empty in email templates

SOLVE

Hey @MNick2 

There is the "type" function that will function similarly to typeof in JS.

In your case its important to remember that date fields are nullable meaning these type of values are designed to hold a null value representing the absence of a value. Thus an "empty" date property will be null to represent the no date.

 

{{type(contact.added_to_our_list_after) == "null"}} {# true #}

 

 

You could also force a string value and check the length:

 

{% set temp_val = contact.added_to_our_list_after|string %}
{% if temp_val|length <=0 %} {# true %}
…

 

 

 

Hope this gets you going!

Kevin Cornett - Sr. Solutions Architect @ BridgeRev

View solution in original post

6 Replies 6
MNick2
Participant

Check if contact property is empty in email templates

SOLVE

Yup, have done that.

 
Returns nothing, empty string is my assumption. Wish there was a TYPEOF or something.
 
But the property itself, is a empty string, where we can enter anything into it like "added after newsletter" etc, just a basic string.
 
But when it's empty, nothing there, no conditional ideas are working in email templates to check if it's empty.
0 Upvotes
Jaycee_Lewis
Community Manager
Community Manager

Check if contact property is empty in email templates

SOLVE

Hey, @MNick2 👋 I have a few additional troubleshooting questions for you:

— Have you tried adding a temporary debugging line in your template to output the value of contact.added_to_our_list_after to see what is actually being returned? This may help you understand why the logic might be falling into the else statement.

<p>Debug: {{ contact.added_to_our_list_after }}</p>

 

— Ensure that contact.added_to_our_list_after is being returned as a string. Sometimes date fields might behave differently, especially if they are being treated as Date objects. If so, 

comparing it against a default date might help:

{% if contact.added_to_our_list_after == '1970-01-01T00:00:00Z' or contact.added_to_our_list_after == '' or contact.added_to_our_list_after is none %}

 

If you've already ruled these out, or they don't help, please let me know, and we can tag in a few of our community members who are experts with HubL.

 

Talk soon! — Jaycee


HubSpot’s AI-powered customer agent resolves up to 50% of customer queries instantly, with some customers reaching up to 90% resolution rates.
Learn More.


Did you know that the Community is available in other languages?
Join regional conversations by changing your language settings !
0 Upvotes
MNick2
Participant

Check if contact property is empty in email templates

SOLVE
Yup, have done that.

Returns nothing, empty string is my assumption. Wish there was a TYPEOF or
something.

But the property itself, is a empty string, where we can enter anything
into it like "added after newsletter" etc, just a basic string.

But when it's empty, nothing there, no conditional ideas are working in
email templates to check if it's empty.
0 Upvotes
Jaycee_Lewis
Community Manager
Community Manager

Check if contact property is empty in email templates

SOLVE

Thanks @Kevin-C 🙌


HubSpot’s AI-powered customer agent resolves up to 50% of customer queries instantly, with some customers reaching up to 90% resolution rates.
Learn More.


Did you know that the Community is available in other languages?
Join regional conversations by changing your language settings !
Kevin-C
Solution
Recognized Expert | Partner
Recognized Expert | Partner

Check if contact property is empty in email templates

SOLVE

Hey @MNick2 

There is the "type" function that will function similarly to typeof in JS.

In your case its important to remember that date fields are nullable meaning these type of values are designed to hold a null value representing the absence of a value. Thus an "empty" date property will be null to represent the no date.

 

{{type(contact.added_to_our_list_after) == "null"}} {# true #}

 

 

You could also force a string value and check the length:

 

{% set temp_val = contact.added_to_our_list_after|string %}
{% if temp_val|length <=0 %} {# true %}
…

 

 

 

Hope this gets you going!

Kevin Cornett - Sr. Solutions Architect @ BridgeRev
Jaycee_Lewis
Community Manager
Community Manager

Check if contact property is empty in email templates

SOLVE

Hey, @Anton @alyssamwilie @Stephanie-OG, do you have any additional troubleshooting tips for @MNick2?

 

Thank you! — Jaycee


HubSpot’s AI-powered customer agent resolves up to 50% of customer queries instantly, with some customers reaching up to 90% resolution rates.
Learn More.


Did you know that the Community is available in other languages?
Join regional conversations by changing your language settings !
0 Upvotes