Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Need help with a script in script runner please

Chris Scheepers
Community Champion
March 5, 2022

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 .  

1 answer

0 votes
Chris Scheepers
Community Champion
March 5, 2022

Please see some details bellow :

fields.png

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

Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 6, 2022

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. 

Chris Scheepers
Community Champion
March 6, 2022

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 ?

 

code errors.png 

 

code errors2.png

 

Please advise 

Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 7, 2022

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)

Chris Scheepers
Community Champion
March 8, 2022

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 :

error code 3.png

Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 8, 2022

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)

Like Chris Scheepers likes this
Chris Scheepers
Community Champion
March 8, 2022

@Nic Brough -Adaptavist- 

 

You are a Legend .

Thank you so much.

That did the trick !!!!

Like Nic Brough -Adaptavist- likes this
Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 9, 2022

No problem, sorry I put you on to the wrong variable type at first!

Suggest an answer

Log in or Sign up to answer