Velocitip: Convert was-Boolean, now-String → Number, then enjoy $display.message

Level 10 - Community Moderator

Boolean type fields enjoy a strange place in Marketo's Velocity implementation.

On the Marketo UI side and via most Marketo APIs, Booleans hold consistent true/false values (presumably using SQL bit type fields under the hood).

But in Velocity, as I've explored before, a Boolean field on the Lead/Person is presented as one of two Strings:

"1" for Boolean true

"" (empty string) for Boolean false

That conversion is quite confusing in its own right (I don’t know of any other system that does that particular stringification) but even more confusing is how Velocity deals (or not) with it.

Remember, Velocity gets a lot of things from Java.

But in Java, there’s no such thing as a Boolean-ish value with truth-iness and false-iness qualities. There are only real Booleans. If you try to use a non-Boolean in a Boolean context, you get a fatal error.

Velocity strives to be an easier-to-use dialect of Java which throws fewer errors. Its way of avoiding this particular error is a simple rule: anything other than null or the real Boolean false is truthy. The empty String and any non-empty String are both truthy!

So if you do:

#if( $lead.someBooleanField )
Field is true
Field is false

You will always get “Field is true” even if you've unchecked the corresponding checkbox in the Marketo UI.

If Marketo Booleans were Velocity/Java Booleans, on the other hand, this would've worked fine. But alas.

So the question is what to do with these sort-of-Boolean-like-Strings (whose meaning we understand, but the VTL language doesn't). What do we convert them to to make them more usable?

You can do a step-by-step conversion to Boolean by doing a String comparison:

#if(  $lead.someBooleanField.equals("1") )
#set( $lead.someBooleanField == true )
#set( $lead.someBooleanField == false )

After that conversion, #if($field) will work as expected.

But I've been thinking lately that maybe converting those Strings to Numbers — 0for false and 1 for true, as the standard goes — gives us more juice within Velocity than if we were to go with true Booleans.

Read the full post on TEKNKL :: Blog →