Troubleshooting Velocity Scripting: Introduction

Kevin_Tuttle1
Marketo Employee
Marketo Employee

Through Dynamic Content, Marketo Engage offers a very powerful way to personalize emails and landing pages based on the properties of a Person. Creating Segments allows each Person to be filed into one (and only one) group for a given parameter. A common use case is for personalizing an email based on the language requested by the recipient. Segments can be setup (left) and then used to customize the content of content modules (right).

Kevin_Tuttle1_0-1654038093612.png Kevin_Tuttle1_1-1654038799003.png

 

As you can see, when you have only eight different options, they are all mutually exclusive, and each completely changes the entire content of the block, using Segments with Dynamic Content is a great solution.

 

But what if the personalization was about something far more important:

Kevin_Tuttle1_2-1654040057105.png

 

You could set up Segments for the various preferred flavors:

Kevin_Tuttle1_3-1654040102946.png

But to which Segment would you assign the record above? Vanilla? Rocky Road? If you pick one or the other then you're losing information and that Person would always get personalized content for one or the other based on which was ranked higher in priority. You might create an additional Segment called "Vanilla + Rocky Road" just for those that like both, but making one Segment for every possible combination would get unwieldy in a hurry. And if the difference between the two in the content is pretty small, it may not even be worth it.

 

For situations like this, Marketo Engage offers special Email Script tokens that implement Apache's Velocity Engine - a Java-based scripting language, not, confusingly, to be mistaken for JavaScript.

 

Kevin_Tuttle1_4-1654040738077.png

Upon creating the Token, and clicking "Click to Edit", an editor pops up with a place for the script on the left, and a list of existing custom fields and objects on the right. The Marketo Developers site has documentation with common examples available, and this won't be a general tutorial on Velocity Scripting, but rather tips on the development process, troubleshooting errors, and some common pitfalls for the starting coder.

 

A functional, if not particularly compelling, example of a script that uses our custom field could be this:

Kevin_Tuttle1_5-1654041147485.png

You can see that building a script for this field allows for multiple different matching scenarios to happen simultaneously. While the Dynamic Content could only say one or the other, with the scripts, the marketer can empathize equally with those that like vanilla and those that like chocolate. And here is the first gotcha...


Gotcha #1: Make sure your field is checked on the right

This is one of the most common errors that I see, especially when copying scripts from one Token to another. You must ensure that any fields referenced on the left are checked on the right. Pasting in code will not automatically check any fields in the code itself. Being checked tells the rendering engine that it needs to have that value available and if it isn't checked then your script will have just an empty value for that field.

 

This is true whether you are selecting standard fields, custom fields (as above), or fields in custom Marketo objects. Any referenced field has to be selected to work.

 

4029
2
2 Comments
SanfordWhiteman
Level 10 - Community Moderator

@Kevin_Tuttle1 wouldn’t advise using contains() directly on the String value because it can lead to unexpected results when subvalues have text in common.

 

Rather, use a proper split() on the string, then use contains() on the resulting Array. Velocity coerces the Array into a List so it has all the List methods.

 

I explored this exact behavior here: https://nation.marketo.com/t5/product-blogs/brainteaser-why-does-this-line-of-velocity-code-work/ba-... and here: https://nation.marketo.com/t5/product-blogs/february-s-brainteaser-unteased-here-s-why-that-bit-of-v....

 

Jo_Pitts1
Level 10 - Community Advisor

I get this is just a worked example, but I'm not sure about repeating #if statements either.