Custom email module does not evaluate IF/ELSE correctly

SOLVE
ArtLinkov
Participant

Hi,

I have a bit of an issue with configuring an email module with an IF/ELSE statement base on a transactional email template.

 

I know all the custom variables we send are properly added to the email (the debug print works), but whatever I do I cannot use any of them to trigger the IF logic correctly.

Attached is a screenshot of the email I'm getting where you can see that the 'nextCharge' parameter is correctly set as 'true' but the 'nextCharge|bool' is not being processed correctly returning and empty '{}'

 

Screenshot at 17-03-20.png

 

I even went as far as creating a macro for that, with no success...

 

Code I'm using in the module:

<!-- isEnabledForEmailV3Rendering: true -->

{% macro test(nextChargeDate, nextCharge) %}
  nextChargeDate: {{ nextChargeDate|pprint }}<br>
  nextCharge: {{ nextCharge|pprint }}<br>
  nextCharge|bool: {{ nextCharge|bool|pprint }}<br>

  {% if nextCharge|bool == true %}
  We will attempt to charge your card one more time, the next attempt will be on {{   nextChargeDate }}.
  {% else %}
  This was the third and last charge attempt.
  {% endif %}
{% endmacro %}

{{ test(custom.nextChargeDate, custom.nextCharge) }}

 

-------------------------------------

Here are the custom properties I'm using for testing:

 

nextCharge = true

"customProperties": {

"fullName": "John Doe",

"last4": "1234",

"transactionAmount": "100.0$",

"transactionDate": "2021-06-09T16:03:59Z",

"nextChargeDate": "2021-06-16T16:03:59Z",

"nextCharge": "true"

}

 

nextCharge = false

 

"customProperties": {

"fullName": "John Doe",

"last4": "1234",

"transactionAmount": "100.0$",

"transactionDate": "2021-06-09T16:03:59Z",

"nextChargeDate": "",

"nextCharge": "false"

}

-------------------------------------

 

If anyone encountered this issue before, I would really appreciate an advice.

 

Thanks,

Art

0 Upvotes
1 Accepted solution

Accepted Solutions
Teun
Solution
Guide | Diamond Partner

Hi @ArtLinkov , I had a similar issue when trying to use if / else statements. All I needed to do to fix my issue was adding the following beta: https://knowledge.hubspot.com/email/create-programmable-emails

I see you've added the right template tag in your code, but is the beta active in the portal that you are using?

Did my answer solve your issue? Help the community by marking it as the solution.

View solution in original post

6 Replies 6
Teun
Solution
Guide | Diamond Partner

Hi @ArtLinkov , I had a similar issue when trying to use if / else statements. All I needed to do to fix my issue was adding the following beta: https://knowledge.hubspot.com/email/create-programmable-emails

I see you've added the right template tag in your code, but is the beta active in the portal that you are using?

Did my answer solve your issue? Help the community by marking it as the solution.

View solution in original post

Teun
Guide | Diamond Partner

@ArtLinkov Does you 'if statement' work if you change it to:

  {% if nextCharge == 'true' %}
  We will attempt to charge your card one more time, the next attempt will be on {{   nextChargeDate }}.
  {% else %}
  This was the third and last charge attempt.
  {% endif %}

So without the use of the `|bool` filter?

Did my answer solve your issue? Help the community by marking it as the solution.
0 Upvotes
ArtLinkov
Participant

Thanks @Teun that did the trick!

0 Upvotes
Indra
Top Contributor | Diamond Partner

Hi @ArtLinkov,

 

The |bool filter is only needed to check inside an if statement. printing the value won't work.  Also it's not possible to |pprint the value from it. If you want to set something, you should use it inside the 'if' statement.

So the best approach will be:

nextCharge|bool: {{ nextCharge|pprint }}

If you want to setup conditional logic this can be done as the bool documentation:

 

{% if nextCharge|bool == true %}
  This is true
{% else %}
  This is false
{% endif %}

 

 

I checked your sample and I got the 'Else' value for your second property.

 

My code was like:

<!-- isEnabledForEmailV3Rendering: true -->

{% macro test(nextChargeDate, nextCharge) %}
{% set bool = nextCharge %}
  bool : <pre>{{ nextChargebool|pprint }}</pre><br>
  nextChargeDate: {{ nextChargeDate|pprint }}<br>
  nextCharge: {{ nextCharge|pprint }}<br>
  nextCharge|bool: {{ bool|pprint }}<br>

  {% if nextCharge|bool == true %}
  We will attempt to charge your card one more time, the next attempt will be on {{ nextChargeDate }}.
  {% else %}
  This was the third and last charge attempt.
  {% endif %}
{% endmacro %}



{% set nextChargeDate = '2021-06-16T16:03:59Z' %}
{% set nextCharge = 'true' %}

{{ test(nextChargeDate, nextCharge) }}
<hr>
<pre style="display:block;">
"customProperties": {
"fullName": "John Doe",
"last4": "1234",
"transactionAmount": "100.0$",
"transactionDate": "2021-06-09T16:03:59Z",
"nextChargeDate": "{{ nextChargeDate }}",
"nextCharge": "{{ nextCharge }}"
}
</pre>
{% set nextChargeDate = '' %}
{% set nextCharge = 'false' %}

{{ test(nextChargeDate, nextCharge) }}
<hr>
<pre style="display:block;">
"customProperties": {
"fullName": "John Doe",
"last4": "1234",
"transactionAmount": "100.0$",
"transactionDate": "2021-06-09T16:03:59Z",
"nextChargeDate": "{{ nextChargeDate }}",
"nextCharge": "{{ nextCharge }}"
}
</pre>

Bureau Vet

Did my post solve your question? Help the community by marking it as a solution
ArtLinkov
Participant

Thanks @Indra it did help me with debugging another issue I had!

webdew
Top Contributor | Diamond Partner

Hi @ArtLinkov ,

Yes, you can develop this module in Hubspot. First, you can create the custom card listing using Hubspot looping. https://developers.hubspot.com/docs/cms/building-blocks/modules
then add the style using CSS. In the module, you can't able to add the CSS but you can add your custom CSS in the email template header section.


Hope this helps!


If we were able to answer your query, kindly help the community by marking it as a solution.

Thanks and Regards.

0 Upvotes