SOLVED

Boolean Values/Logic in Velocity?

Go to solution
nhabischWings
Level 5

Boolean Values/Logic in Velocity?

Hello,

Curious what I'm missing when it comes to boolean data types in Custom Objects. We're importing through the API into a Boolean data type with a 1 (TRUE) 0 (FALSE) approach. Also noticed it seems if the value is NULL, Marketo calls it "false".

With this in mind, why does this not work:

#foreach ($item in $customerCardList)
#if ( ( ($item.AuthorizedUserOnAccount.equals("0")) ||($item.AuthorizedUserOnAccount.isEmpty()) ) && ($item.cardTypeDetails.equals("SIGNATURE")))
No Authorized User
#else
I can't script apparently.
#end
#end


If I understand, if I am looking for a "false" Boolean value I see if it is a 0 OR an empty value. When I test the above against a Lead with a record that matches the above - it still gives me the "else" output.

Is there some other way to account for a false Boolean value? Or is it better to play against a TRUE value, like "if X IS NOT 1, show this"?

2 ACCEPTED SOLUTIONS

Accepted Solutions
Darshil_Shah1
Level 10 - Community Advisor + Adobe Champion

Re: Boolean Values/Logic in Velocity?

Well, you've come across a little quirk of how velocity processes boolean values. In short, the problem is that both "1" and "" (empty string) are processed as being a true value in Velocity. Try the below script:

 

#set( $mktoBoolean = { "1" : true, "" : false } )
#foreach ($item in $customerCardList)
#if ( !$mktoBoolean[$item.AuthorizedUserOnAccount] && $item.cardTypeDetails.equals("SIGNATURE"))
No Authorized User
#else
I can't script apparently.
#end
#end

 

You can also house the enum $mktoBoolean in a separate global token, so you don't need to remember to include it in all the scripts where you're working with boolean fields.

 

P.S. - You should definitely check out Sandy's article discussing this in very detail here; very informative, as always. 🙌

View solution in original post

SanfordWhiteman
Level 10 - Community Moderator

Re: Boolean Values/Logic in Velocity?


In regards to this code: $item.AuthorizedUserOnAccount.equals("0")
If $item.AuthorizedUserOnAccount is being imported as a string and not an integer, then using .equals("0") could work, but it's not necessary if you're only dealing with integer-based Boolean values (0 and 1). Directly comparing to 0 is simpler and more efficient in this case.

Double equals == isn’t “directly” comparing at all. It’s the opposite because it’s a coercive comparison, and leads to unexpected results.

 

You should always use the typesafe .equals(), not ==. When comparing to the empty string, it’s .equals(""). When comparing to the numeric string “0” it’s .equals("0"). When comparing to the numeric 0 it’s .equals(0)

View solution in original post

8 REPLIES 8
TyrellC
Level 2

Re: Boolean Values/Logic in Velocity?

Hey @nhabischWings 👋


I would suggest trying this:

#foreach ($item in $customerCardList)
    #if ((($item.AuthorizedUserOnAccount == 0) || $item.AuthorizedUserOnAccount.isEmpty()) && $item.cardTypeDetails.equals("SIGNATURE"))
        No Authorized User
    #else
        I can't script apparently.
    #end
#end


I switched $item.AuthorizedUserOnAccount.equals("0") to ($item.AuthorizedUserOnAccount == 0)

In regards to this code: $item.AuthorizedUserOnAccount.equals("0")
If $item.AuthorizedUserOnAccount is being imported as a string and not an integer, then using .equals("0") could work, but it's not necessary if you're only dealing with integer-based Boolean values (0 and 1). Directly comparing to 0 is simpler and more efficient in this case.


I hope this helps!

Tyrell Curry
Marketo Web Developer @ MERGE
SanfordWhiteman
Level 10 - Community Moderator

Re: Boolean Values/Logic in Velocity?


In regards to this code: $item.AuthorizedUserOnAccount.equals("0")
If $item.AuthorizedUserOnAccount is being imported as a string and not an integer, then using .equals("0") could work, but it's not necessary if you're only dealing with integer-based Boolean values (0 and 1). Directly comparing to 0 is simpler and more efficient in this case.

Double equals == isn’t “directly” comparing at all. It’s the opposite because it’s a coercive comparison, and leads to unexpected results.

 

You should always use the typesafe .equals(), not ==. When comparing to the empty string, it’s .equals(""). When comparing to the numeric string “0” it’s .equals("0"). When comparing to the numeric 0 it’s .equals(0)

TyrellC
Level 2

Re: Boolean Values/Logic in Velocity?

Thanks for the insight @SanfordWhiteman! Good to know.

Tyrell Curry
Marketo Web Developer @ MERGE
Darshil_Shah1
Level 10 - Community Advisor + Adobe Champion

Re: Boolean Values/Logic in Velocity?

Well, you've come across a little quirk of how velocity processes boolean values. In short, the problem is that both "1" and "" (empty string) are processed as being a true value in Velocity. Try the below script:

 

#set( $mktoBoolean = { "1" : true, "" : false } )
#foreach ($item in $customerCardList)
#if ( !$mktoBoolean[$item.AuthorizedUserOnAccount] && $item.cardTypeDetails.equals("SIGNATURE"))
No Authorized User
#else
I can't script apparently.
#end
#end

 

You can also house the enum $mktoBoolean in a separate global token, so you don't need to remember to include it in all the scripts where you're working with boolean fields.

 

P.S. - You should definitely check out Sandy's article discussing this in very detail here; very informative, as always. 🙌

nhabischWings
Level 5

Re: Boolean Values/Logic in Velocity?


@Darshil_Shah1 wrote:

Well, you've come across a little quirk of how velocity processes boolean values. In short, the problem is that both "1" and "" (empty string) are processed as being a true value in Velocity. Try the below script:

 

#set( $mktoBoolean = { "1" : true, "" : false } )
#foreach ($item in $customerCardList)
#if ( !$mktoBoolean[$item.AuthorizedUserOnAccount] && $item.cardTypeDetails.equals("SIGNATURE"))
No Authorized User
#else
I can't script apparently.
#end
#end

 

Okay maybe I misread, but according to the above - shouldn't it read:

#set( $mktoBoolean = { "1" : true, "" : true } )

 Since you stated that Velocity processes both 1 and "" as a TRUE value?

Darshil_Shah1
Level 10 - Community Advisor + Adobe Champion

Re: Boolean Values/Logic in Velocity?

Well, no because with the enum usage, we intend to fix that little quirk (within the script locally, of course), so for the scope of the enum, and where it's used to check values, the intended (and logical values) are considered in the velocity, i.e., "" (empty string) = false and not true.

SanfordWhiteman
Level 10 - Community Moderator

Re: Boolean Values/Logic in Velocity?


Also noticed it seems if the value is NULL, Marketo calls it "false".

Booleans in Marketo are only nullable (tri-state) in the raw database column.

 

In practice, you’re going to be dealing with them in an output context like {{lead.tokens}} or Velocity references. In such contexts they’re implicitly bi-state, only holding some version of true or some version of false.

nhabischWings
Level 5

Re: Boolean Values/Logic in Velocity?

Ah okay, so in the context of a Velocity script, there's no need to account for a NULL/empty state since it will only reflect a value of TRUE (actually true) or FALSE (actually false or NULL)?