Re: Email Conditionals

Mal_Warwick
Level 1

Email Conditionals

Hello,

I'm running into a bit of an issue when trying to apply conditionals. when it reaches the else condition, the lead.First Name is left blank when testing the email.

  #if ({{lead.First Name:default=Friend}})

  Testt

  #else

  {{lead.First Name}},  Testt

  #end

if some one has some pointers, greatly appreciated.

5 REPLIES 5
SanfordWhiteman
Level 10 - Community Moderator

Re: Email Conditionals

You seem to be mixing together different syntax.

In Velocity, variables (references) are denoted with a $dollarSign or only if proven necessary a ${formalReference} style with a dollar sign + single curly braces.

Your script should look like this:

#if( $lead.FirstName.isEmpty() )

Hi, Friendo!

#else

Hi, ${lead.FirstName}!

#end

Note how I used the formal reference style only in the line that creates output. You shouldn't use it anywhere else.

Mal_Warwick
Level 1

Re: Email Conditionals

Hi Sanford,

Thank you for the reply, would I need to set this script into My Tokens? I'm pretty new to Marketo and have tried my best to look over documentation and follow the tutorials. apprecieate the help.

SanfordWhiteman
Level 10 - Community Moderator

Re: Email Conditionals

Yes, you'd create an "Email Script" type of {{my.token}} (which is written in Velocity Template Language and also called a "Velocity token").

In the tree of fields on the right-hand side of the Script Editor window, make sure you've checked off FirstName, and any other fields you wish to process in Velocity. If you don't check them off, they aren't present and will always be seen as null.

pastedImage_0.png

Then you include the {{my.token}} in your email as with other tokens.

I can't recommend strongly enough that you read my posts on Velocity, both those on the official Products blog and additional posts (not cross-posted here to not overwhelm/overbore people) at https://blog.teknkl.com/tag/velocity.

Finally, when posting code to Community threads, always highlight it using the Advanced Editor's syntax highlighter:

https://s3.amazonaws.com/blog-images-teknkl-com/syntax_highlighter.gif

There's no highlighter specific to Velocity, but you can choose Java (not JavaScript) which is intimately related.

Mal_Warwick
Level 1

Re: Email Conditionals

Great to hear from you Sanford and thanks for sharing on how to share my code but I don't have that option when replying.

2018-11-29_11-05-41.png

So I modified the script example you suggested and created a token for pretty much each folder directory. I ran a test and was not able to get it to populate. any suggestions why that step I'm missing?

#if( $lead.FirstName.isEmpty() )

Friend,

#else

${lead.FirstName}, Than You!

#end

Here is the token script

2018-11-29_10-49-03.png

and html

2018-11-29_11-07-17.png

SanfordWhiteman
Level 10 - Community Moderator

Re: Email Conditionals

Great to hear from you Sanford and thanks for sharing on how to share my code but I don't have that option when replying.

2018-11-29_11-05-41.png

Open the thread, then reply, then click Advanced Editor (this isn't available on mobile).

So I modified the script example you suggested and created a token for pretty much each folder directory.

Please don't do that, you'll just be endlessly confused when your tokens are overridden. You need one token, at the highest possible level in Marketing Activities. Make sure to clear all your redundant tokens out ASAP.

I ran a test and was not able to get it to populate. any suggestions why that step I'm missing?

What did populate in the email, then?

Does your email exist in the Marketing Activities tree under a program/folder that has the {{my.token}} in it?