SOLVED

Re: Velocity scripting - How to store multiple variables in a single token?

Go to solution
Anonymous
Not applicable

Velocity scripting - How to store multiple variables in a single token?

Hi there,

For each email I'm creating I have multiple email scripts because the CTA link, hero image, etc. all depend on variables (in my case the user's city)

Is it possible to store all of this in one single token instead of having one token per variable I need?

What I'd like to do is something like:

#if(${lead.City} == "Montréal")

  #set( $thisButtonCityLinkEn = "http://www.yellowpages.ca/pl/loc/montreal-qc")

  #set( $thisButtonCityLinkFr = "http://www.pagesjaunes.ca/pl/loc/montreal-qc")

  #set( $thisHeroImage = "bla bla Montreal Hero bla bla")

  #set( $thisCleanCity = "Montreal")

#elseif(${lead.City} == "Toronto")

  #set( $thisButtonCityLinkEn = "http://www.yellowpages.ca/pl/loc/toronto-on")

  #set( $thisButtonCityLinkFr = "http://www.pagesjaunes.ca/pl/loc/toronto-on")

  #set( $thisHeroImage = "bla bla Toronto Hero bla bla")

  #set( $thisCleanCity = "Toronto")

#end

And then from the inside of the email I would like to be able to call the 4 different variables I need for each email. Is this possible or does each Email Script return only 1 value? Or perhaps an array of values?

Thanks,

Thomas

Tags (1)
1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity scripting - How to store multiple variables in a single token?

You can easily store the data in a single Velocity variable (I strongly recommend the use of maps/dictionary objects in VTL, wherever you can)...

#set( $localData = {

  "thisButtonCityLink" : {

     "en" : "http:​//www.yellowpages.ca/pl/loc/montreal-qc",

     "fr" : "http:​//www.pagesjaunes.ca/pl/loc/montreal-qc"

  }

} )

... and this variable will be available from all subsequent tokens present in the email.

But to get different output, you have to either:

1. Use different tokens (that each output different properties from the same global variable).

or

2. Mutate the variable each time you output the (same) token, as I demonstrated in this blog post​. This approach is not for the faint of heart as it means the order of tokens directly changes the output. It's kind of like working in some long-ago programming language where each use of a variable "pops the stack." You might find it useful, though.

View solution in original post

15 REPLIES 15
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity scripting - How to store multiple variables in a single token?

You can easily store the data in a single Velocity variable (I strongly recommend the use of maps/dictionary objects in VTL, wherever you can)...

#set( $localData = {

  "thisButtonCityLink" : {

     "en" : "http:​//www.yellowpages.ca/pl/loc/montreal-qc",

     "fr" : "http:​//www.pagesjaunes.ca/pl/loc/montreal-qc"

  }

} )

... and this variable will be available from all subsequent tokens present in the email.

But to get different output, you have to either:

1. Use different tokens (that each output different properties from the same global variable).

or

2. Mutate the variable each time you output the (same) token, as I demonstrated in this blog post​. This approach is not for the faint of heart as it means the order of tokens directly changes the output. It's kind of like working in some long-ago programming language where each use of a variable "pops the stack." You might find it useful, though.

Anonymous
Not applicable

Re: Velocity scripting - How to store multiple variables in a single token?

Thanks for that Sanford. I agree, using dictionaries is great. So how would I then call this variable from the inside of my email to display the English thisButtonCityLink ?

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity scripting - How to store multiple variables in a single token?

${thisButtonCityLink['en']}

To parameterize with the lead's preferred language (assuming you have such a field), like so:

${thisButtonCityLink[$lead.PreferredLanguage]}

Anonymous
Not applicable

Re: Velocity scripting - How to store multiple variables in a single token?

Hi Sanford,

I'm still working on this... and trying to use the dictionary solution. So I've created an Email Script in my tokens which goes like this:

Screen Shot 2017-07-25 at 2.45.19 PM.png

(I've checked the correct information in the right hand panel)

But when I then call merchantName in the email subject, it doesn't get processed:

Screen Shot 2017-07-25 at 2.46.19 PM.png

Are all the Email scripts processed in the program before the email asset is built? Or will the email only use the email scripts that are called?

I'm thinking that my email script is not being processed because I'm not callint it,which is why the merchant name doesn't appear in the subject. If this is the case, how do I call an email script in an email, just to initialize the different variables? Or should I use that email script to at least display the first variable in the email after initializing the variables?

Thanks,

Thomas

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity scripting - How to store multiple variables in a single token?

  • You have to include a token for it to be processed.
  • Tokens are processed from top to bottom of the email.  Variables set in earlier tokens can be called from later tokens, but not vice versa.
  • You don't put VTL directly in the email content.  Your Subject line needs to be in another Velocity token, then you include that token as {{my.subject}}.
Anonymous
Not applicable

Re: Velocity scripting - How to store multiple variables in a single token?

Thank you for your patience, Sanford.

I created a mkteditable section in my template, I edit the email asset and insert my token in the zone. But when I try to approve the email asset, I get this error:

Screen Shot 2017-07-25 at 3.24.48 PM.png

Any idea what I'm doing wrong?

Am I using the correct syntax for thisMerchantData? Because the MID is not the same color as the Name. Or is it the Marketo parser that is not consistent?

Thanks,

Thomas

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity scripting - How to store multiple variables in a single token?

You left out the "$" before the variable name in those lines.

Anonymous
Not applicable

Re: Velocity scripting - How to store multiple variables in a single token?

Hi Sanford,

Ok thanks I fixed that. I'm pulling my hair out right now trying to make this work. I'll try to detail this as much as I can.

I've created an Email Script token called {{my.Merchant Data}} in which there is:

#set($size = $mAAPITest3_cList.size())

#set($lastCustomObject = $size - 1)

#set( $localData = { 

  "thisMerchantData" : { 

     "Name" : ${mAAPITest3_cList.get($lastCustomObject).merchantName},

     "MID" : ${mAAPITest3_cList.get($lastCustomObject).merchantID}

  } 

} ) 

And I've checked the checkboxes for the 2 values I'm fetching from Custom Objets.

I call this token in my subject, like so:

Screen Shot 2017-07-25 at 4.35.38 PM.png

{{my.subject}} is an Email Script token that contains (just for testing purposes): ${thisMerchantData['Name']}

In the email body I call:

{{my.Merchant Name}} which is also an Email Script token and also contains: ${thisMerchantData['Name']}

None of these are replaced by the value I'm trying to extract from the last Custom Object:

Screen Shot 2017-07-25 at 4.39.08 PM.png

Help! I don't have much hair left

Is $localData a special name in Velocity or did you just make it up? What is the hierarchy between localData and thisMerchantData? One of them is the dictionary name?

Thanks for all your help, Sanford!

Thomas

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity scripting - How to store multiple variables in a single token?

{{my.subject}} is an Email Script token that contains (just for testing purposes): ${thisMerchantData['Name']}

But that's not a valid reference. (That's why you just see the literal VTL text, since Velocity "gracefully" outputs that when it encounters such an error.)

thisMerchantData is a property of the top-level $localData object. Name is a property of the thisMerchantData object. Thus

$localData["thisMerchantData"]["Name"]

or just

$localData.thisMerchantData.Name

(Since these properties don't have spaces, you can use dot-notation.)

$localData a special name in Velocity or did you just make it up?

It's not special, just a local variable name.