Hi everyone,
I have some issues with velocity and email scripting. Hope that you can tell me what I'm doing wrong.
Guess you know the example from https://developers.marketo.com/email-scripting showing how to use a simple if construction.
Trying to use the constructor copy and paste does not work so I tried to adapt it due to the documentation but without success.
#if(${lead.Salutation} == "Mr")
#set($greeting = "Dear Mr. ${lead.LastName}")
#else
#set($greeting = "Error: $lead.Salutation unknown")
#end
$greeting
The result of this code is "Dear Mr. ${lead.LastName}" but the Last Name is not injected into the string.
Trying the following adaptions brought the same result. (The second without the brackets )
#set($greeting = "Dear Mr. " +${lead.LastName})
#set($greeting = "Dear Mr. " +$lead.LastName)
So I was not able to bring the value of the database into the string,
Second Issue:
I tried to use .equals() instead of "==" but this threw an error.
#if(${lead.Salutation}.equals("Mr"))
"Sehr geehrter Herr ${lead.LastName},"
#else
${lead.Salutation}
#end
Threw the following error
Cannot get email content- <div>An error occurred when procesing the email Body! </div> <p>Lexical error, Encountered: "e" (101), after : "." at *unset*[line 375, column 27] near</p> <div><pre ><div class="mktoText" id="text"> </pre><pre ><p>#if(${lead.Salutation}.equals("Mr"))</pre><pre class="x-form-item-label"> "Sehr geehrter Herr ${lead.LastName},"</pre><pre >#else</pre><pre > ${lead.Salutation}</pre></div>
Do you have an idea what went wrong here?
Unfortunately, the examples in the docs have several errors/suboptimal practices.
I tried to use .equals() instead of "==" but this threw an error.
#if(${lead.Salutation}.equals("Mr")) "Sehr geehrter Herr ${lead.LastName}," #else ${lead.Salutation} #end
In this particular case you should be using simple references, not formal references.
#if(${lead.Salutation} == "Mr") #set($greeting = "Dear Mr. ${lead.LastName}") #else #set($greeting = "Error: $lead.Salutation unknown") #end $greeting
The result of this code is "Dear Mr. ${lead.LastName}" but the Last Name is not injected into the string.
Most likely because you didn’t check off Last Name in the tree on the right-hand-side of Script Editor.
I recommend you read all my Products Blog posts on Velocity and the additional posts at https://blog.teknkl.com/tag/velocity. These are the seminal sources of information on how to code Velocity correctly for the Marketo context.