Hello Community!
I am reviewing how to use Velocity Script for modifying tokens, content in emails, etc. I came across a script that looks like it might be useful in solving a current problem.
We are using a token {{Lead.Sub Partner}} to identify various Sub Partner classes, i.e. Presidential, Chairman's Club, Select, etc.
The problem is that when some of our contacts were imported into Marketo from an excel document, the apostrophe got transposed into some unnecessary characters: Marriott - Chairman’s Club.
Would it be possible to use the script below to change this Sub Partner name to this? Marriott - Chairman's Club?
#set( $lead.SubPartner )
#if( $lead.SubPartner contains("Marriott - Chairman’s Club") )
Marriott - Chairman's Club
#end
Or am I way off here?
Thank you much,
LK
Solved! Go to Solution.
I am reviewing how to use Velocity Script for modifying tokens, content in emails, etc. I came across a script that looks like it might be useful in solving a current problem.
First — I wouldn’t use the expression “modifying tokens”. A Velocity token is a {{my.token}}. You probably mean “modifying field values” (I changed your post title for clarity).
Velocity can certainly modify how fields appear in email content. That’s one of the things it’s for: formatting and localizing dates and currencies, changing string capitalization, summing numbers, etc.
However, this particular situation you’re talking about isn’t one of “unnecessary” characters.
It’s that someone chose the wrong text encoding type when creating an import, so instead of the single UTF-8 character apostrophe (same as right single curly quote ’, not the same as straight single quote ') it was seen as 3 separate characters in Windows-1252.
This specific character sequence ’ could be replaced by ’ using .replace() since it’s unlikely those 3 chars were intended to be printed as-is, right next to each other.
But this isn’t going to fix all other encoding problems, ultimately you need to fix them at the source. Once something has been read in the wrong encoding, you can’t get the original encoding back out.
I am reviewing how to use Velocity Script for modifying tokens, content in emails, etc. I came across a script that looks like it might be useful in solving a current problem.
First — I wouldn’t use the expression “modifying tokens”. A Velocity token is a {{my.token}}. You probably mean “modifying field values” (I changed your post title for clarity).
Velocity can certainly modify how fields appear in email content. That’s one of the things it’s for: formatting and localizing dates and currencies, changing string capitalization, summing numbers, etc.
However, this particular situation you’re talking about isn’t one of “unnecessary” characters.
It’s that someone chose the wrong text encoding type when creating an import, so instead of the single UTF-8 character apostrophe (same as right single curly quote ’, not the same as straight single quote ') it was seen as 3 separate characters in Windows-1252.
This specific character sequence ’ could be replaced by ’ using .replace() since it’s unlikely those 3 chars were intended to be printed as-is, right next to each other.
But this isn’t going to fix all other encoding problems, ultimately you need to fix them at the source. Once something has been read in the wrong encoding, you can’t get the original encoding back out.
Hi Sanford,
I was able to fix the sourcing issue, but now we have a request to modify the field values.
Currently, the text appears as "Marriott - Executive," "Marriott - Presidential," "Marriott - Chairman's Club," etc.
Our team member would instead like the text to appear in emails without the "Marriott - " so it reads: Executive, Presidential, Chairman's Club.
Is there a script that would allow me to accomplish the field value modification?
Thank you,
LK
#set( $valueWithoutMarriottPrefix = $yourStringValue.replaceFirst("Marriott - ","") )
Thank you Sanford.
I am seeing this error when I try to preview how the text will appear in an email.
This is how I entered the code in the email script token I created:
#set( $valueWithoutMarriottPrefix = ${lead.subPartner}.replaceFirst("Marriott - ","") )
#end
Any ideas what the issue might be?
Thank you,
LK
Make sure you follow my examples more closely, there are no curly braces in my code nor an #end.
#set( $valueWithoutMarriottPrefix = $lead.subPartner.replaceFirst("Marriott - ","") )
Sorry about that!
I copied/pasted your code directly into the script. It appears that the entirety of the Sub Partner name is being removed now, for some reason:
Where the token should appear, it's showing as blank: "As a (blank) member, you get...". I only previewed contacts that have the "Marriott - " followed by additional text (i.e. Presidential, Executive, etc.).
Is it possible to replace the entire value for this {{Lead.Sub Partner}} field with completely different text? Something like the below? Or would this not work for my scenario?
#if(${lead.subpartner} == “Marriott - Presidential“)
a Presidential
#elseif (${lead.subpartner} == “Marriott - Executive”)
an Executive
#elseif (${lead.subpartner} == “Marriott - Chairman’s Club”)
a Chairman’s Club
#elseif (${lead.subpartner} == “Marriott - Select Club”)
a Select
#elseif (${lead.subpartner} == “Marriott - Owner”)
an Owner
#else
Marriott
#end
Thank you,
LK
Are you ever outputting $valueWithoutMarriottPrefix? A #set doesn’t output anything. It just creates the new variable.
#if(${lead.subpartner} == “Marriott - Presidential“) a Presidential #elseif (${lead.subpartner} == “Marriott - Executive”) an Executive ...
Don‘t do it this way, please. It’s very poor programming practice.
Also, you should use .equals(), not ==, in any case. And those curly quotes will never compile. You must use straight quotes.
Yes, there is a Sub Partner name that is just "Marriott" with no Marriott - prefix.
Yes, there is a Sub Partner name that is just "Marriott" with no Marriott - prefix.
I don’t understand what you’re responding to here.
I will circle back to my original question.
I used the code you provided, but it removed the "Marriott - Chairman's Club" content when I previewed the email using a contact's email address, instead of just showing "Chairman's Club".
For example, I entered this person in (see first screenshot below) who has the "Marriott - Chairman's Club" Sub Partner, and this is how the token value appears in the email (second screenshot):
I entered the script like this:
#set( $valueWithoutMarriottPrefix = $lead.subPartner.replaceFirst("Marriott - ","") )
Can you please help me figure out how to just show whatever comes after the prefix?
Thank you,
LK
As asked above, where are you outputting $valueWithoutMarriottPrefix?
Your code is only setting it now.
You have to output variables if you want to see their value.
The $valueWithoutMarriottPrefix should be output between "As a" and "member" as shown in the copy below.
The {{my.Sub Partner Token Test:default=edit me}} token is what I created to contain the script that you provided.
As a {{my.Sub Partner Token Test:default=edit me}} member, you get {{lead.enrollment amount}}.
You need a line in your token which outputs $valueWithoutMarriottPrefix.
${valueWithoutMarriottPrefix}
Otherwise, you are only setting it. It’s natural for a token with no output to have no output!
Awesome, thank you!
This is the code that I entered:
#set( $valueWithoutMarriottPrefix = $lead.subPartner.replaceFirst("Marriott - ","") )
Chairman's Club
I tested using a contact whose Sub Partner is "Marriott - Chairman's Club".
Here's what I was able to see in the email:
How would I set up the logic to present a different output (for example, Presidential) if the contact's Sub Partner field = "Marriott - Presidential"?
LK
Doesn’t seem like you’re following what that #set is doing. It’s setting a new variable.
That new variable is derived from the value of $lead.subPartner. The “Marriott - “ prefix has been stripped.
Then you must output that new variable to see it:
${valueWithoutMarriottPrefix}
Have to recommend you take things a bit slower with Velocity if this still doesn’t make sense, since supporting Velocity tokens in production means you have to be able to jump into debug mode if something breaks.
I believe that I am following you but not understanding how to output the new variable.
The #set is setting a new variable that removes the "Marriott -" prefix and then displays everything that appears after that.
For example, if the value for $lead.subPartner is "Marriott - Presidential," the new variable would be "Presidential."
I've been trying to find more information online, but am not seeing any helpful resources.
I came across this article which has an interesting example (below) that shows where #set is used to set the $foo variable as "Velocity" and then results in "Hello Velocity World!" being printed on a webpage.
But I'm not sure how to apply this similar use case to print the ${valueWithoutMarriottPrefix} variable in the email.
<html>
<body>
#set( $foo = "Velocity" )
Hello $foo World!
</body>
</html>
The result is a web page that prints "Hello Velocity World!".
I had tried that before--entering the code exactly as you provided--but it was not rendering in the email.
I just tried again, and it worked finally.
Thank you for your help!
Hi Sanford,
If I wanted to append "a" or "an" to the output depending on if the Sub Partner begins with a consonant or vowel, how would I go about doing that?
For example, if the output is "Executive," I would want to append "an," as in "an Executive".
If the output is "Select," I would want to append "a," as in "a Select".
Thank you,
LK
$lead.subPartner.replaceAll("^Marriott - ","").replaceAll("^","a ").replaceAll("(?i)^a ([aeiou])","an $1")