SOLVED

Modifying field values using velocity script?

Go to solution
Guitarrista82
Level 6

Modifying field values using velocity script?

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

1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Modifying field values using velocity script?


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.

View solution in original post

28 REPLIES 28
SanfordWhiteman
Level 10 - Community Moderator

Re: Modifying field values using velocity script?


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.

Guitarrista82
Level 6

Re: Modifying field values using velocity script?

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

 

 

SanfordWhiteman
Level 10 - Community Moderator

Re: Modifying field values using velocity script?

#set( $valueWithoutMarriottPrefix = $yourStringValue.replaceFirst("Marriott - ","") )
Guitarrista82
Level 6

Re: Modifying field values using velocity script?

Thank you Sanford.

 

I am seeing this error when I try to preview how the text will appear in an email. 

Screen Shot 2021-07-07 at 11.47.31 AM.png

 

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

SanfordWhiteman
Level 10 - Community Moderator

Re: Modifying field values using velocity script?

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 - ","") )

 

Guitarrista82
Level 6

Re: Modifying field values using velocity script?

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:

 

Screen Shot 2021-07-07 at 12.32.12 PM.png

 

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

 

SanfordWhiteman
Level 10 - Community Moderator

Re: Modifying field values using velocity script?

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.

Guitarrista82
Level 6

Re: Modifying field values using velocity script?

Yes, there is a Sub Partner name that is just "Marriott" with no Marriott - prefix.

SanfordWhiteman
Level 10 - Community Moderator

Re: Modifying field values using velocity script?


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.