Velocity script breaking when using "#,###" | Community
Skip to main content
Level 2
January 28, 2025
Solved

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

  • January 28, 2025
  • 2 replies
  • 1019 views

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?

 

Best answer by SanfordWhiteman

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

2 replies

SanfordWhiteman
Level 10
January 28, 2025

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
January 28, 2025

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!

Level 2
January 28, 2025
#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
SanfordWhitemanAccepted solution
Level 10
January 28, 2025

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