Velocity tokens for Math Equations | Community
Skip to main content
Travis_Schwartz
New Participant
November 29, 2019
Solved

Velocity tokens for Math Equations

  • November 29, 2019
  • 1 reply
  • 5112 views

Hello all,

I'm hoping I can get a little help with some Velocity scripting.

 

Essentially I I have a token that populates for certain people and I am trying to reduce that rate (it's a token) by either .25 or .50. doing some research and I found this site (Velocity Math Tool: Subtract : MathTool « Velocity « Java), so I plugged in the following:

 

$math.sub($lead.autoPreApprovalNewRate, ".25")

 

and

$math.sub($lead.autoPreApprovalNewRate, ".50")

 

I solved the main issue (not rendering the token but rather displaying the code) by confirming that the checkbox was clicked (it was not). now I guess my question is, is this the ideal way to perform such a task?

Thanks!

Best answer by SanfordWhiteman

In general, subtracting a floating-point value of .25 or .50 is bad practice. But the alternative is very complex to build in Velocity, and I wouldn't worry about the difference.

I would, however, guard it a bit more:

#if( $lead.autoPreApprovalNewRate.toString().equals("") )
#set( $lead.autoPreApprovalNewRate = 0 )
#end
$math.sub($lead.autoPreApprovalNewRate, ".50")‍‍‍‍

This is because a null numeric value could be an empty string in Velocity. I would do this even if you're filtering out people who have a null value, because you never want to expect source code to be visible to the end user.

1 reply

SanfordWhiteman
SanfordWhitemanAccepted solution
New Participant
November 29, 2019

In general, subtracting a floating-point value of .25 or .50 is bad practice. But the alternative is very complex to build in Velocity, and I wouldn't worry about the difference.

I would, however, guard it a bit more:

#if( $lead.autoPreApprovalNewRate.toString().equals("") )
#set( $lead.autoPreApprovalNewRate = 0 )
#end
$math.sub($lead.autoPreApprovalNewRate, ".50")‍‍‍‍

This is because a null numeric value could be an empty string in Velocity. I would do this even if you're filtering out people who have a null value, because you never want to expect source code to be visible to the end user.

Travis_Schwartz
New Participant
November 29, 2019

Yeah, this is why I asked.   This isn't how I wanted to go about it, but it seems like for it to get done, it has to be via some math operation in Marketo and this was the cleanest thing I could find.

The list that these are going out to are all have a value for this field. So hopefully it's not a problem.

If I were to change it to:

#set( $lead.autoPreApprovalNewRate = Contact us for your rate )


would that make it so that it would display that message instead of showing a zero? I would rather it give them a message vs saying their rate was 0. Also I'm noticing that if I plug in the code you have provided and plug in someones email address who isn't part of the list of people who have this code on their record, it's showing a -.50 is that to be expected? is that because it's setting it to zero and subtracting the .50?

SanfordWhiteman
New Participant
November 29, 2019

Oh no, that wouldn't work at all (it's an unquoted string, so a syntax error for one, and you can't subtract from a string anyway).

I would rather it give them a message vs saying their rate was 0.

Then do an #if/#else/#end.

would that make it so that it would display that message instead of showing a zero? I would rather it give them a message vs saying their rate was 0. Also I'm noticing that if I plug in the code you have provided and plug in someones email address who isn't part of the list of people who have this code on their record, it's showing a -.50 is that to be expected? is that because it's setting it to zero and subtracting the .50?

Yes, it'd be up to you to show a different value, the important thing is you don't throw an error.