Hi Guys,
I am new to script runner and I am not a developer . I need some help to return a calculation , but it seems like something goes wrong somewhere.
When a number like 199 is input in the text field everything works and my custom fields returns values . As soon as I use a number like 199.50 the fields don't return anymore .
Please see some details bellow :
The following fields are text fields (the reason it is text is so that nrs are not rounded off) :
Share Price T
Broker Commission
And Last Commission is also a text field.
Here is my code
def *iorcom = getCustomFieldValue("*ior Commission %") as long
def Quantity = getCustomFieldValue("Quantity (N)") as long
def Price = getCustomFieldValue("Share Price (T)")
def BPS = getCustomFieldValue("Commission")
def Price1 = Price as long
def BPS1 = BPS as long
if (BPS) {
def total = (Price1 * Quantity)
def calc = (BPS1 / 100)
def calc1 = (total * calc)
def basepts = (calc1 / 100)
def *ior_fin = (basepts * *iorcom / 100)
return *ior_fin
}
else {
return null
}
Please note I entered some *'s to protect my clients name in above code
Get rid of the forced variable types (the "as" bits). The whole point of "def" is that it decides what the type of the variable is.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Good Morning Nic,
Thanks for responding.
I have removed the as bits as you suggested . I think I am now running into the problem that one can not do calculations on text fields ?
Please advise
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Ok, so you have numbers in text fields, the trick there is that you'll need to read them out as numbers (you can't just wedge strings into number fields, they need to be interpreted)
From memory, the easiest thing to do is use the parse functions in Java - assuming BPS is a string that contains a written number, try
def calc = ( Long.parseLong(BPS) / 100)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Nic,
Still no luck :-(
I entered the Long.parseLong before all the text fields . It works when there are no , or . in the numeric values in the text field. as soon as the price field has a . in it , it fails with this error :
Script field failed on issue: OV-42, field: *ior Commission (SF) java.lang.NumberFormatException: For input string: "200.50"
My edited script looks like this now as you advised :
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I am really sorry, I've told you some total junk there!
Well, the idea is right, but I've picked the wrong method. Longs are integers, they can't handle decimals.
So, I'm sorry to have to make you re-do a load of edits, but try:
def calc = ( Double.parseDouble(BPS) / 100)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
No problem, sorry I put you on to the wrong variable type at first!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.