CMS Development

Merb01
Participant

How to tell if a contact's date property is empty

Hi everyone! I am trying to find out how to tell if a contact has a date property filled in or not. Basically we want to hide some text if this date field does not have a value. I have tried some things that work with regular text fields but do not seem to work for a date field.

 

Here are my tests:

 

{% set effective_date = contact.plan_effective_date %}

<p>effective_date: {{ effective_date }}</p>
<p>effective_date pprint: {{ effective_date|pprint }}</p>
<p>effective_date == '': {{ effective_date == '' }}</p>
<p>effective_date == null: {{ effective_date == null }}</p>
<p>effective_date|string == '': {{ effective_date|string == '' }}</p>
<p>effective_date is undefined: {{ effective_date is undefined }}</p>
<p>effective_date is none: {{ effective_date is none }}</p>

 

I have one contact with the "plan_effective_date" empty (contact A) and one that has a value of "05/01/2022" (contact B)

 

The result for contact A (no value in date field)

 

effective_date:
effective_date pprint: (String: )
effective_date == '': false
effective_date == null: false
effective_date|string == '': false
effective_date is undefined: false
effective_date is none: false

 

 

The result for contact B (contact with a value of "05/01/2022")

 

effective_date: 5/1/22
effective_date pprint: (String: 5/1/22)
effective_date == '': false
effective_date == null: false
effective_date|string == '': false
effective_date is undefined: false
effective_date is none: false

 

 

EDIT: I was expecting at least one of the tests for contact A to be true. As it is an empty value.

 

The only difference is that I can print out the date when it exists, but if there is not value set then I still get all falses. It seems the logical checks are finding a value but nothing is printing out if there is no value set to the field.

 

Any ideas here? Thank you!

0 Upvotes
Kevin-C
Recognized Expert | Partner
Recognized Expert | Partner

How to tell if a contact's date property is empty

Hey @Merb01 

 

Ahh I remember!

 

So a date picker property is a datetime object which is a value type, so it cannot be null. Think of it like Int or String.

 

Without going to far into the details of this you've got 2 options:

  1. Format the value as datetime and compare to today() formatted as datetime
  2. Test the type of the property

See examples below:

This code:

{# Print the value using "datetimeformat" becuase we know its a datetime object #}
{{ contact.hs_lifecyclestage_customer_date|datetimeformat('%B %e, %Y')  }}<br>

{# Compare the value to today #}
{{ contact.hs_lifecyclestage_customer_date == today() }}<br>

{# Format and compare the value to today #}
{{ contact.hs_lifecyclestage_customer_date|datetimeformat('%B %e, %Y') == today()|unixtimestamp|datetimeformat('%B %e, %Y') }}<br>

{# test type #}
{{ type(contact.hs_lifecyclestage_customer_date) }}<br>

Renders this output:

KevinC_0-1642875352168.png

 

If its not obvious I would choose option 2 over the inevitable headache of option 1!

 

Merb01
Participant

How to tell if a contact's date property is empty

thank you so much @Kevin-C for your help on this. I am not sure what I am doing differently but I do not get the same results as you.

Have a "date picker" property/field we created (named "plan_effective_date"). I have set today's date in it "Jan 1, 2022" on a contact (Contact A), and nothing set on another contact (Contact B).

 

My adjusted code:

 

{% set effective_date = contact.plan_effective_date %}
<p>effective_date: {{ effective_date }}</p> 

<p>type(effective_date): {{ type(effective_date) }}</p>

<p>effective_date|datetimeformat('%B %e, %Y'): {{ effective_date|datetimeformat('%B %e, %Y') }}</p>

<p>effective_date == today(): {{ effective_date == today() }}</p>

<p>effective_date pprint: {{ effective_date|pprint }}</p>

<p>effective_date == '': {{ effective_date == '' }}</p>

<p>effective_date == null: {{ effective_date == null }}</p>

<p>effective_date is undefined: {{ effective_date is undefined }}</p>

<p>effective_date is none: {{ effective_date is none }}</p>

<p>effective_date|string == '': {{ effective_date|string == '' }}</p>

{% if effective_date %}
<p>If statment 'effective_date': {{ effective_date }}</p>
{% endif %}

 

 

Contact A result (has date of today set "Jan 28, 2022")

effective_date: 1/28/22

type(effective_date): str

effective_date|datetimeformat('%B %e, %Y'):

effective_date == today(): false

effective_date pprint: (String: 1/28/22)

effective_date == '': false

effective_date == null: false

effective_date is undefined: false

effective_date is none: false

effective_date|string == '': false

If statment 'effective_date': 1/28/22

 

Contact B result (nothing set to the field):

effective_date:

type(effective_date): str

effective_date|datetimeformat('%B %e, %Y'):

effective_date == today(): false

effective_date pprint: (String: )

effective_date == '': false

effective_date == null: false

effective_date is undefined: false

effective_date is none: false

effective_date|string == '': false

If statment 'effective_date':

 

I see with Contact A that I can get "1/28/22" out but when I try to use "datetimeformat" nothing is printed out. and "effective_date == today()" comes out as False?

 

Some more information if this helps. This is in a custom created email template and I am testing mostly in the email preview under "actions" and selecting my test contacts. I also get the same results every time I have sent a test email to one of my test contacts.

 

Again thank you so much for your help on this Kevin-C

 

0 Upvotes
Merb01
Participant

How to tell if a contact's date property is empty

Hey @Kevin-C thank you for your reply and I ran a new test with your suggestion but it seems to be working the same way in that even when the value is empty (nothing in the date property on the contact) it still registers as true in an if statment.

New test code:

{% set effective_date = contact.plan_effective_date %}
<p>effective_date: {{ effective_date }}</p>
<p>effective_date pprint: {{ effective_date|pprint }}</p>
<p>effective_date == '': {{ effective_date == '' }}</p>
<p>effective_date == null: {{ effective_date == null }}</p>
<p>effective_date|string == '': {{ effective_date|string == '' }}</p>
<p>effective_date is undefined: {{ effective_date is undefined }}</p>
<p>effective_date is none: {{ effective_date is none }}</p>

{% if effective_date %}
    <p>If statment effective_date: {{ effective_date }}</p>
{% endif %}

 

Contact A result (no value on date picker property)

effective_date:
effective_date pprint: (String: )
effective_date == '': false
effective_date == null: false
effective_date|string == '': false
effective_date is undefined: false
effective_date is none: false
If statment effective_date:

I was expecting the "if statment effective_date" text to not display

 

Contact B result (has a value of May 1st, 2022 in the date property)

effective_date: 5/1/22
effective_date pprint: (String: 5/1/22)
effective_date == '': false
effective_date == null: false
effective_date|string == '': false
effective_date is undefined: false
effective_date is none: false
If statment effective_date: 5/1/22

 

Thank you so much for any help on this!

0 Upvotes
Kevin-C
Recognized Expert | Partner
Recognized Expert | Partner

How to tell if a contact's date property is empty

Hey @Merb01 

 

What happens if you wrap the expression in a if statement?

 

{% if effective_date %}
  {{ effective_date }}
{% endif %}