Hey!
We're currently using velocity script tokens for dynamic content on our emails and currently having some challenges with getting the script to show a dollar amount with the $ format.
I was expecting the field lead.yearlyAmount to spit out the amount with the $ sign (field type is currency) but I guess that's not how it works so we've tried using the script below to no avail. The script doesn't seem to be parsing - any ideas?
#set($yearlyCurrency = $number.toNumber($lead.yearlyAmount))
$esc.dollar$number.format('#,###', $yearlyCurrency)
Any help would be appreciated!
Jervis
Solved! Go to Solution.
First: you should never use Currency -- for anything -- despite the name. For round numbers, use Integer. For numbers with decimals, use scaled Integers (value * 100) and scale them back in Velocity or JS.
Also, please highlight code when posting using the syntax highlighter so it's readable. Highlight as Java as it's closest to VTL.
You don't need to convert to a Number (Double) separately, $number.format() does that conversion internally. Make sure you escape all your special Velocity symbols.
${number.format("${esc.d}${esc.h},${esc.h}${esc.h}${esc.h}", $lead.yearlyAmount)}
First: you should never use Currency -- for anything -- despite the name. For round numbers, use Integer. For numbers with decimals, use scaled Integers (value * 100) and scale them back in Velocity or JS.
Also, please highlight code when posting using the syntax highlighter so it's readable. Highlight as Java as it's closest to VTL.
You don't need to convert to a Number (Double) separately, $number.format() does that conversion internally. Make sure you escape all your special Velocity symbols.
${number.format("${esc.d}${esc.h},${esc.h}${esc.h}${esc.h}", $lead.yearlyAmount)}
How can I apply that same scripting to show a $ sign and a comma in an amount. For example, I tried to apply it this way, but no luck. (I am a very novice user)
${number.format("${esc.d}${esc.h},${esc.h}${esc.h}${esc.h}", $lead.APPBURSELAMNT)}
Thanks @SanfordWhiteman I appreciate the help, but it's still not rendering properly. I'm sure it's user error so I opened a ticket.
@SanfordWhiteman I do have another question now. I'm trying to use this script 3 times in my email with different data fields, but I can only get the original one to work. Do I have to do something different for each individual token? Or what am I doing wrong? I really appreciate the help as I learn velocity scripting.
Original
${number.format("${esc.d}${esc.h},${esc.h}${esc.h}${esc.h}", $lead.APPBURSELAMNT)}
#2
${number.format("${esc.d}${esc.h},${esc.h}${esc.h}${esc.h}", $lead.APPBURSELPMT)}
#3
${number.format("${esc.d}${esc.h},${esc.h}${esc.h}${esc.h}", $lead.APPTWN)}
After I set up the tokens and attempted to preview my data in the email, I received this preview error.
Cannot get email content- <div>An error occurred when procesing the email Body! </div> <p>Encountered ")" near</p> <div><pre ></pre></div>
I didn't see any errors in my SB with three different email script tokens added in the email, each referencing the data from three different fields. Do you have any other email script added in the email apart from these three lines? Apologies if I'm missing out on anything here. Thanks!
No, I have nothing else.
Agreed, the error is somewhere else.
Be aware that if you’re trying to use a literal dollar sign, even outside of a formal Velocity {{my.token}}, you should use the Velocity escape ${esc.d}.
Let's try again. Here is the full error when I attempt to approve my email.
Not Allowed
Validation Error approving CTA all campaigns_2022_06.ectaD2 — <div>An error occurred when procesing the email Body! </div> <p>Encountered ")" near</p> <div><pre >Based on your application, it looks like you currently pay </pre><pre ><b>${number.format("${esc.d}${esc.h},${esc.h}${esc.h}${esc.h}",${lead.navientAPPBURSELPMT)}</b>&nbsp;each month on a total balance of&nbsp;</pre><pre class="x-form-item-label"><strong>${number.format("${esc.d}${esc.h},${esc.h}${esc.h}${esc.h}", $lead.navientAPPBURSELAMNT)}</strong>. Here are the different ways you can save:</pre><pre ></div></td> </pre><pre ></tr> </pre></div>
|
You’ve reversed the closing curly brace and closing comma.
It is a preview with a real email address populated that has the correct data in it.
That is the correct syntax to output
12345
as
$12,345
so we’d need to know what you mean by “no luck”.
WAIT, I got it. I knew it was user error! Thank you!
I entered the script into my token exactly as you edited it. The number still shows up without a comma and will has the ".0" at the end which is odd because the data coming into Marketo has the comma and .00
Worked perfectly! Was doing my head in for a good few weeks.Thank you Sanford Whiteman!
*also edited to highlight code
Thanks a lot for the highlighting, helps others remember.