• Learn how AI and automation actually work in your Help Desk. Ask our experts how to improve team speed and customer happiness! AMA Nov 17-21.

    Ask us anything

CMS Development

darveesh
Contributor

Cannot string compare in custom module

SOLVE

Pretty frustrating day here.  I want to accomplish the following:  In the body of our marketing email, if the contact has a salutation then put a phrase "Hello [Salutation] [Last Name]".  If the contact does not have a salutation value then no phrase is used.  I made a custom module to drag and drop in the email editor. Here is the HubL:

{% if contact.salutation|striptags|trim|length > 0 %}
Debug: {{contact.salutation|striptags|length}}
<p>Hi [{{ contact.salutation }}] [{{ contact.lastname|default("") }}],</p>
{% endif %}


When I preview the email with a contact that does or does not have a salutation, the debug statements gets output.  Go figure.

Without salutation:
Debug: 22

Hi [] [Quinn],

With salutation:
Debug: 22

Hi [Mr.] [Smith],



Can anyone help make heads or tails out of this?  What magical filter do I need to use to be able to check the actual salutation field value?

0 Upvotes
2 Accepted solutions
alyssamwilie
Solution
Recognized Expert

Cannot string compare in custom module

SOLVE

Ugh, I see now that it was rendering correctly for me in the editor, but not in the email preview. It's very possible that if statements are also only available in programmable emails though it doesn't state such on the operators documentation page. : /

 

Edit: Yep, I just threw the code into a programmable email module and it worked as expected. So operations and expressions are limited to programmable emails. Very frustrating that they have no documentation to clearly spell out what is and not available for email.

If this answer solved your question, please mark it as the solution.

Alyssa Wilie Profile Image

Alyssa Wilie

Web Developer at Lynton

Learn HubL | Get Marketing Insights

Lynton's HubSpot theme Rubric now available. Click to download.

View solution in original post

0 Upvotes
darveesh
Solution
Contributor

Cannot string compare in custom module

SOLVE

@alyssamwilie, based on your line of thinking, I saw there is a beta to make the module for programmable email.  When I enable that, all the logic works.  It seems that beta doesn't limit the module just to be used in automated emails as far as I can tell. So this is the alternate answer: Must make the module for programmable emails (but use it in regular emails).

 

darveesh_0-1759517243618.png

 

View solution in original post

0 Upvotes
18 Replies 18
alyssamwilie
Recognized Expert

Cannot string compare in custom module

SOLVE

As stated in the documentation filters cannot be used on personalization tokens in email UNLESS you use a programmable email module. 

https://knowledge.hubspot.com/marketing-email/create-programmable-emails

 

Note this is only available in portals with Marketing Hub Professional or Enterprise.

If this answer solved your question, please mark it as the solution.

Alyssa Wilie Profile Image

Alyssa Wilie

Web Developer at Lynton

Learn HubL | Get Marketing Insights

Lynton's HubSpot theme Rubric now available. Click to download.
0 Upvotes
darveesh
Contributor

Cannot string compare in custom module

SOLVE

Hi @alyssamwilie .  Thank you for pointing that note! Given the article you point out that filters are however not supported,  do you think a basic comparison should work?  Without the use of any filters:

 

{% if contact.salutation != "" %}
Hi {{ contact.salutation }} {{ contact.lastname }} ,
{% else %}
{% endif %}

 

Because the above doesn't work either.  No matter if the contact.salutation has a value or not it will go into the IF statement.  Now, one could say that contact.salutation is not the real string value of that property and has invisible tags and such (which can't be removed via a filter).  Is that the (lame) resolution?  As a typical use case, we can't even do a compare statement?

0 Upvotes
darveesh
Contributor

Cannot string compare in custom module

SOLVE

Another approach without filters.  The temp variable get set to "none" but the compare for it fails. Is that normal programming behavior?

 

{% set salutation = personalization_token('contact.salutation', 'none') %}
Salutation is: [{{salutation}}]<br/>
{% if salutation == "none" %}
{% else %}
Hi {{ contact.salutation }} {{ contact.lastname }} ,
{% endif %}

 

The saluation variable outputs none, but the compare fails and goes into the statment. Example output for a contact without a salutation:

 

Salutation is: [none]
Hi Ryan ,

0 Upvotes
alyssamwilie
Recognized Expert

Cannot string compare in custom module

SOLVE

Even when the property is technically empty HubSpot's email renderer weirdly returns a string like "CONTACT.SALUTATION" and there are also situtations where "null" may be returned. So to efficiently use an if statement for these properties in email it would need to be something like:

{% if contact.salutation != "CONTACT.SALUTATION" && contact.salutation != null %}
    Hi {{ contact.salutation }} {{ contact.lastname }} ,
{% else %}
{% endif %}

  

If this answer solved your question, please mark it as the solution.

Alyssa Wilie Profile Image

Alyssa Wilie

Web Developer at Lynton

Learn HubL | Get Marketing Insights

Lynton's HubSpot theme Rubric now available. Click to download.
0 Upvotes
darveesh
Contributor

Cannot string compare in custom module

SOLVE

Thank you @alyssamwilie for staying with me.  I tried your script to check for null and default values, but the IF statement still goes through and outputs: 

 

Hi Ryan ,

 

Did you get a chance to look at the follow up post I made, where personalization_token correctly identifies an empty saluation and returns its fallback value.  But when I simply compare on this fallback value - note I did not use blank as fallback value on purpose - it fails:  https://community.hubspot.com/t5/CMS-Development/Cannot-string-compare-in-custom-module/m-p/1207890/....  That should not be normal no?  A string compare against a constant string is failing.

0 Upvotes
alyssamwilie
Recognized Expert

Cannot string compare in custom module

SOLVE

Hm, it worked fine on my end. Are you using {{ contact.salutation }} directly or the personalization_token() function like in your other example? Because the function does NOT return a direct string, it returns an object expression.

Screenshot 2025-10-03 115738.png

 

For a direct comparison you have to use the flat variable as shown in my previous example, not the personalization_token function.

If this answer solved your question, please mark it as the solution.

Alyssa Wilie Profile Image

Alyssa Wilie

Web Developer at Lynton

Learn HubL | Get Marketing Insights

Lynton's HubSpot theme Rubric now available. Click to download.
0 Upvotes
darveesh
Contributor

Cannot string compare in custom module

SOLVE

@alyssamwilie, so perhaps personalization token can't be used since it returns an object and needs filters to process which we can't use.   The current expression I have is pretty much what you provided:

 

{% if contact.salutation != "CONTACT.SALUTATION" && contact.salutation != null && contact.salutation != '' %}
    Hi {{ contact.salutation }} {{ contact.lastname }} ,
{% else %}
{% endif %}

 

And for me it produces the "Hi..." not matter if the contact has a saulation or not:

 

Hi Hoover ,

 

I never get the ELSE (no output) for any contact.   But if that's working for you, then the mystery deepens. Some beta you or I are/aren't in?

0 Upvotes
alyssamwilie
Solution
Recognized Expert

Cannot string compare in custom module

SOLVE

Ugh, I see now that it was rendering correctly for me in the editor, but not in the email preview. It's very possible that if statements are also only available in programmable emails though it doesn't state such on the operators documentation page. : /

 

Edit: Yep, I just threw the code into a programmable email module and it worked as expected. So operations and expressions are limited to programmable emails. Very frustrating that they have no documentation to clearly spell out what is and not available for email.

If this answer solved your question, please mark it as the solution.

Alyssa Wilie Profile Image

Alyssa Wilie

Web Developer at Lynton

Learn HubL | Get Marketing Insights

Lynton's HubSpot theme Rubric now available. Click to download.
0 Upvotes
darveesh
Solution
Contributor

Cannot string compare in custom module

SOLVE

@alyssamwilie, based on your line of thinking, I saw there is a beta to make the module for programmable email.  When I enable that, all the logic works.  It seems that beta doesn't limit the module just to be used in automated emails as far as I can tell. So this is the alternate answer: Must make the module for programmable emails (but use it in regular emails).

 

darveesh_0-1759517243618.png

 

0 Upvotes
matt_scott
Top Contributor | Platinum Partner
Top Contributor | Platinum Partner

Cannot string compare in custom module

SOLVE

Hey @darveesh My only thought would be to use the render filter which should help make sure the final value is outputted rather than any intermediarry variables or information
https://developers.hubspot.com/docs/cms/reference/hubl/filters#render
Also sometimes seperating it into a var helps, I've not tested it however:

{% set salutation = contact.salutation|striptags|trim|render %}
{% if salutation|length > 0 %}
Debug: {{salutation|striptags|length}}
<p>Hi [{{ contact.salutation }}] [{{ contact.lastname|default("") }}],</p>
{% endif %}



Matthew Scott
Head of Development & Hubspot Solutions Architect | Deeply Digital

B2B marketing agency: Specialist B2B content marketing and demand generation for SaaS vendors and HubSpot Users | Deeply Digital | HubSpot Partner since 2010


01926 334003

deeplydigital.co.uk

3 Morton Street, Leamington Spa, CV32 5SY, UK
0 Upvotes
matt_scott
Top Contributor | Platinum Partner
Top Contributor | Platinum Partner

Cannot string compare in custom module

SOLVE

may need to do the render first thinking about it

Matthew Scott
Head of Development & Hubspot Solutions Architect | Deeply Digital

B2B marketing agency: Specialist B2B content marketing and demand generation for SaaS vendors and HubSpot Users | Deeply Digital | HubSpot Partner since 2010


01926 334003

deeplydigital.co.uk

3 Morton Street, Leamington Spa, CV32 5SY, UK
0 Upvotes
darveesh
Contributor

Cannot string compare in custom module

SOLVE

I tried both approaches: 

{% set salutation = contact.salutation|striptags|trim|render %}
{% if salutation|length > 0 %}
Debug: {{salutation|striptags|length}}
<p>Hello [{{ contact.salutation }}] [{{ contact.lastname|default("") }}],</p>
{% endif %}

 

Produces this for both cases (with and without salutation:

 

Debug: 22
Hello [Mr.] [Smith],

 

Debug: 22
Hello [] [Quinn],

 

 

And this one 

 

{% set salutation = contact.salutation|render|striptags|trim %}
{% if salutation|length > 0 %}
Debug: {{salutation|striptags|length}}
<p>Dear [{{ contact.salutation }}] [{{ contact.lastname|default("") }}],</p>
{% endif %}

 

produces

 

Debug: 22

Dear [Mr.] [Smith],

 

Debug: 22
Dear [] [Quinn],

 

 

The engine is broken I think.

0 Upvotes
STierney
Community Manager
Community Manager

Cannot string compare in custom module

SOLVE

Hey @darveesh - thanks so much for posting in the Community!


I'd like to tag in some experts to see if there's something we can do to achieve this!

@matt_scott, @ChristinaKay, and @Danielle_J - any thoughts for @darveesh?

Shane, Community Manager





loop


Loop Marketing is a new four-stage approach that combines AI efficiency and human authenticity to drive growth.

Learn More




0 Upvotes
darveesh
Contributor

Cannot string compare in custom module

SOLVE

Appreciate it.  Unless I am way off course, I think there is a big bug here.

albertsg
Key Advisor

Cannot string compare in custom module

SOLVE

Hi @darveesh

A quick thought, If {{salutation}} is a string, have you tried comparing its content instead of its length? 

For example, check if it's not empty:

{% if contact.salutation|striptags|trim != '' %}


Maybe it's also worth ensuring it's a string. You can use |pprint to see what type of variable it is and if it's not a string then use |string to convert it. 

0 Upvotes
darveesh
Contributor

Cannot string compare in custom module

SOLVE

Hi @albertsg.  I tried this as well but it suffers from the same underlying issue. Here is the latest debug script:

 

Personalization token: [{{ personalization_token('contact.salutation', 'none')}}]<br/>
Personalization token length: [{{ personalization_token('contact.salutation', 'none')|length}}]<br/>
Raw: [{{ contact.salutation }}]<br/>
Raw length: [{{ contact.salutation|length }}]<br/>

{% if contact.salutation|striptags|trim != '' %}
Salutation is: [{{contact.salutation}}]
{% endif %}

 

A contact with salutation result: 

Personalization token: [Mr.]
Personalization token length: [221]
Raw: [Mr.]
Raw length: [22]
Salutation is: [Mr.]

 

 

A contact without salutation result:

Personalization token: [none]
Personalization token length: [221]
Raw: []
Raw length: [22]
Salutation is: []

 

Note that the if statement goes through in either case and prints the "Salutation is: " line.  It should not in one of those cases.

0 Upvotes
albertsg
Key Advisor

Cannot string compare in custom module

SOLVE

Hi @darveesh, are you sure contact.salutation is a string and not an object?
You can try using the |pprint filter.


What happens if you do the following?

{% if contact.salutation %}
  Salutation is: [{{ contact.salutation }}]
{% endif %}


Does it enter the IF statement in both cases as well?  If it's an object, HubL might consider an empty contact.salutation property as a null and therefore won't display the content.


0 Upvotes
darveesh
Contributor

Cannot string compare in custom module

SOLVE

Here is the code I just tried:

 

pprint Salutation: {{ contact.salutation|pprint }}<br/>
{% if contact.salutation %}
  Salutation is: [{{ contact.salutation }}]
{% endif %}

 

Output for a contact without a salutation value:

pprint Salutation: (String: )
Salutation is: []

 

Output for a contact with salutation value:

pprint Salutation: (String: Chief)
Salutation is: [Chief]

 

Yes, the IF statement goes through for both cases as you see above.

0 Upvotes