SOLVED

Re: Modifying field values using velocity script?

Go to solution
Guitarrista82
Level 6

Re: Modifying field values using velocity script?

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):

 

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

 

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

 

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

SanfordWhiteman
Level 10 - Community Moderator

Re: Modifying field values using velocity script?

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.

Guitarrista82
Level 6

Re: Modifying field values using velocity script?

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}}.

SanfordWhiteman
Level 10 - Community Moderator

Re: Modifying field values using velocity script?

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!

Guitarrista82
Level 6

Re: Modifying field values using velocity script?

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:

 

 

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

 

 

 

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

SanfordWhiteman
Level 10 - Community Moderator

Re: Modifying field values using velocity script?

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.

Guitarrista82
Level 6

Re: Modifying field values using velocity script?

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!".

 

 

 

SanfordWhiteman
Level 10 - Community Moderator

Re: Modifying field values using velocity script?

You output it by enclosing it in curly braces, on its own line in the token, as in my examples above.
Guitarrista82
Level 6

Re: Modifying field values using velocity script?

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. 

 

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

 

Thank you for your help!

Guitarrista82
Level 6

Re: Modifying field values using velocity script?

@SanfordWhiteman 

 

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