Using a variable in the calculate string formula
Hi,
I am in a custom business rule.
I have a calculate formula like the following one, which works fine:
api.Data.Calculate("A#Sales1 = A#Sales2 + 50”)
Now let say I want to declare a variable instead of having the 50 typed in the formula string, and pass that to the Calculate, what is the correct syntax to do that? Is it possible?
I would like to do something like this (the one below is not the correct syntax, but it gives an idea of what I am trying to do):
Dim Growth As Decimal = 50
api.Data.Calculate("A#Sales1 = A#Sales2 + Growth”)
Thank you
The safest approach is this:
Api.Data.FormulaVariables.SetDecimalVariable("Growth", 50)
API.data.Calculate("A#Sales1 = A#Sales2 + $Growth”)
This approach avoids possible issues with converting numbers to string - when the user executing it has a different Culture, or when decimals get truncated (hence losing precision). There is a blog post discussing it as part of internationalising an application.