SOLVED

Velocity tokens for Math Equations

Go to solution
Travis_Schwartz
Level 4

Velocity tokens for Math Equations

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!

1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity tokens for Math Equations

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.

View solution in original post

8 REPLIES 8
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity tokens for Math Equations

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
Level 4

Re: Velocity tokens for Math Equations

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
Level 10 - Community Moderator

Re: Velocity tokens for Math Equations

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.

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity tokens for Math Equations

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.

Don't get me wrong, it's very clean and will always work 100% predictably, and it's not a problem with Velocity itself. The problem is that w/fp math, "predictable" doesn't necessarily mean "what you thought when just eyeballing it"!

For example $math.sub(8.20,.25) is (always, predictably) 7.949999999999999. Depending on how you want to round it for output, you probably want to wrap it in a $display.format() or $number.number(). Of course this is the same in JavaScript.

Travis_Schwartz
Level 4

Re: Velocity tokens for Math Equations

Makes sense. 

Another question on a similar topic for this campaign I'm working on. Right now we have currency values being pulled over via token, but there are no commas. so for example if I were to pull the token for a rate, it would come over as something like 1000 or 5000 is there a way I can say "whenever you see this token (which we have setup for the rate) add a commas every three digits"? or are there reasons that you would not recommend doing that? I'm not sure what the reasoning is for it not being in the data to begin with, but I was told there was some reasoning. I can't think of an instance where a rate would require a value after the a period... heck, I have not seen a value with a period in it either... and if I did I would probably just urge them to include the comma if they could include the decimal.

Thanks again for your willingness and participation in this community. Yours is the first voice I look to when anyone posts anything. Several of the issues I've had come up I've been able to solve because of your posts and replies to others. I hope one day I can be able to help people out with this stuff and share the knowledge I've gained from folks like you. so thanks again.

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity tokens for Math Equations

I'm not sure what the reasoning is for it not being in the data to begin with, but I was told there was some reasoning.

You could only have a comma in the value if it were a String field. Commas aren't part of a stored Number.

Ironically, String is better than using the actual Currency datatype, which should never be used for, er, currency.

For number values, the comma is an output choice. $number.number() (in the default locale) inserts commas every 3 digits and does not pad decimals (that is, if there would be nothing after the decimal point, it prints just the whole number).

Thanks again for your willingness and participation in this community. Yours is the first voice I look to when anyone posts anything. Several of the issues I've had come up I've been able to solve because of your posts and replies to others. I hope one day I can be able to help people out with this stuff and share the knowledge I've gained from folks like you. so thanks again.

Thanks, nice to hear.

Travis_Schwartz
Level 4

Re: Velocity tokens for Math Equations

haha good to know. I'll see what we can do about changing our currency fields to strings so that we can do that as it looks unsightly... and someone I respect says it's better anyways


SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity tokens for Math Equations

If you need to do numeric filtering (in Smart Lists), though, you have to have a number. You should use an Integer, not Currency, but still there's a distinct advantage.