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:
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.
@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).
Oct 3, 202511:33 AM - edited Oct 3, 202511:34 AM
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?
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:
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.
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.
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.
@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:
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.
@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).
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:
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
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
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.
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.