SOLVED

Velocity script breaking when using "#,###"

Go to solution
juleary324
Level 1

Velocity script breaking when using "#,###"

Hello, 

 

We are working on a Year in Review email that will pull in customer data through a marekto only field. We are using the code below to pull in all of our data and formatting it to include a comma on the larger numbers: 

#set($YIR_most_thank_yousCapped = ${lead.mostThankYousYIR})
#set( $YIR_most_thank_yousFormatted = $number.format( "${esc.h},${esc.h}${esc.h}${esc.h}", $YIR_most_thank_yousCapped ) )
$YIR_most_thank_yousFormatted

 

As I am discussing this code with our in house developer, he suggested we use "#,###" instead of "${esc.h},${esc.h}${esc.h}${esc.h}" as a best practice using this code below: 

#set($YIR_most_thank_yousCapped = ${lead.mostThankYousYIR})
#if($YIR_most_thank_yousCapped && $YIR_most_thank_yousCapped != "")
  #set($YIR_most_thank_yousFormatted = $number.format("#,###", $YIR_most_thank_yousCapped))
  $YIR_most_thank_yousFormatted
#else
  0
#end 

 

 

When we use this code, however, the email preview breaks. I am wondering why the second code won't register in the email?Screenshot 2025-01-28 at 10.53.31 AM.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity script breaking when using "#,###"

Single quotes and double quotes have different meaning in VTL, unlike in other languages. (Single quotes do not dereference variables, they’re taken literally.)

 

Rather than switching between single and double quotes based on content, standardizing on double quotes and escaping reserved characters inside double quotes is the more portable approach, because you typically do want to dereference variables inside a string, with the sole exception of literal tokens like # and $.

View solution in original post

5 REPLIES 5
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity script breaking when using "#,###"

Can you please highlight your code using the syntax highlighter ("Insert/edit Code Sample") so it's readable? Then we'll continue.

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity script breaking when using "#,###"

Your developer is mistaken and it sounds like they lack Velocity experience. The correct practice is to escape the # character as it has special meaning in VTL.

Certainly not "best practice" to use reserved tokens without escaping – neither in VTL nor any other language!

juleary324
Level 1

Re: Velocity script breaking when using "#,###"

#set($YIR_most_thank_yousCapped = ${lead.mostThankYousYIR}) #set( $YIR_most_thank_yousFormatted = $number.format( "${esc.h},${esc.h}${esc.h}${esc.h}", $YIR_most_thank_yousCapped ) )
 
$YIR_most_thank_yousFormatted

 Above is the code I originially used. 

 

 

#set($YIR_most_thank_yousCapped = ${lead.mostThankYousYIR})
#if($YIR_most_thank_yousCapped && $YIR_most_thank_yousCapped != "")
  #set($YIR_most_thank_yousFormatted = $number.format("#,###", $YIR_most_thank_yousCapped))
  $YIR_most_thank_yousFormatted
#else
  0

The code our developer originally suggested is above. 

 

We have also tested this code and it seems to be working 

#set($YIR_most_thank_yousCapped = $lead.mostThankYousYIR)
#set($YIR_most_thank_yousFormatted = $number.format( '#,###', $YIR_most_thank_yousCapped ) )
$YIR_most_thank_yousFormatted
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity script breaking when using "#,###"

Single quotes and double quotes have different meaning in VTL, unlike in other languages. (Single quotes do not dereference variables, they’re taken literally.)

 

Rather than switching between single and double quotes based on content, standardizing on double quotes and escaping reserved characters inside double quotes is the more portable approach, because you typically do want to dereference variables inside a string, with the sole exception of literal tokens like # and $.

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity script breaking when using "#,###"

P.S. A lead field can never be null in Velocity, so you only need

 

if( !$YIR_most_thank_yousCapped.isEmpty() )