Skip to main content
Level 5
Aug 29, 2023 8:48 PM UTC
Solved

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"?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Darshil_Shah1
Darshil_Shah1Accepted solution
Adobe Champion, Community Advisor, UG Leader
Aug 29, 2023 10:46 PM UTC

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. 🙌

8 replies

TyrellC
Level 2
Aug 29, 2023 10:33 PM UTC

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!

Translate
Tyrell CurryMarketo Web Developer @ MERGE
SanfordWhiteman
User Group Leader
User Group Leader
Aug 29, 2023 11:15 PM UTC

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). 

Translate
TyrellC
Level 2
Aug 30, 2023 3:06 PM UTC

Thanks for the insight @sanfordwhiteman! Good to know.

Translate
Tyrell CurryMarketo Web Developer @ MERGE
Darshil_Shah1
Adobe Champion, Community Advisor, UG Leader
Darshil_Shah1Adobe Champion, Community Advisor, UG LeaderAccepted solution
Adobe Champion, Community Advisor, UG Leader
Aug 29, 2023 10:46 PM UTC

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. 🙌

Translate
Level 5
Aug 30, 2023 1:15 PM UTC

@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?

Translate
Darshil_Shah1
Adobe Champion, Community Advisor, UG Leader
Adobe Champion, Community Advisor, UG Leader
Aug 30, 2023 2:20 PM UTC

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.

Translate
SanfordWhiteman
User Group Leader
User Group Leader
Aug 29, 2023 11:19 PM UTC

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.

Translate
Level 5
Aug 30, 2023 12:55 PM UTC

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)?

Translate