Forums

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

Question about ScriptRunner Script Field - Currently Struggling

MAG-II
Contributor
August 29, 2019

Hello - 

In my Jira project I am attempting to configure a Script Field that involves some basic math. There are 2 custom fields that are involved: "Amount Requested" and "Amount Returned." The Script Field is called "Amount Available."

What I want to happen is for the Script Field (Amount Available) to continuously calculate:

(Amount Returned) - (Amount Requested) = Available Amount

I currently have this script:

import com.atlassian.jira.component.ComponentAccessor
def aval = getCustomFieldValue("Amount Requested")
def bval = getCustomFieldValue("Amount Returned")

return bval - aval

 

So far it seems like this script is calculating the correct math. There is one step that I want to add to take this further though. Is there a way for me to set the Amount Available field to 1000, and then continue with the scripted equation?

In short, what I am ultimately attempting to achieve is:

1000 + (Amount Returned) - (Amount Requested) = Available Amount

 

I am at a loss as to what I can do here (if anything). Would appreciate any help I can get. Thanks.

3 answers

1 accepted

1 vote
Answer accepted
robert Mugabuhamye
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.
August 29, 2019

hello Michael.

why not use another variable ?

def initialvalue = 1000

 

then at the end return initialvalue +bval -aval 

?

Josh_Kochelek__Modus_Create
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.
August 29, 2019

Agreed. If it's a static value then you should just addit to the equation.

Unless I'm missing something... 

0 votes
MAG-II
Contributor
August 30, 2019

I apologize for bringing this up again, but I am running into a new problem. The following is the script I have so far:

 

import com.atlassian.jira.component.ComponentAccessor
def aval = getCustomFieldValue("Amount Requested") as Double
def bval = getCustomFieldValue("Amount Returned") as Double


// check both fields contain valid numeric values and if not set them to 0
if (aval == null) {
aval = 0
}
if (bval == null) {
bval = 0
}
if (aval != null && bval != null){
return 1000 - aval + bval
} else {
// return to some code to indicate a null value in one of the fields
return 0
}

 

With 1 Issue uploaded the equation works great, however, as I upload more Issues a problem occurs. Each Issue I upload adds 1000 more to the Amount Available field. 

Quick example: The first Issue I uploaded with the Amount Requested = 100 and Amount Returned = 10 gave me Amount Available = 910.

I uploaded the second Issue and the Amount Available immediately went up to 1910.

 

Would there be any way possible to have this 1000 in Amount Available only occur 1 time, and remain static for additional Issues that get uploaded?

Josh_Kochelek__Modus_Create
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.
August 30, 2019

@MAG-II  - I'd try this:

import com.atlassian.jira.component.ComponentAccessor
def aval = getCustomFieldValue("Amount Requested") as Double
def bval = getCustomFieldValue("Amount Returned") as Double


// check both fields contain valid numeric values and if not set them to 0
if (aval == null) {
aval = 0
}
if (bval == null) {
bval = 1000
}
if (aval != null && bval != null){
return bval - aval
} else {
// return to some code to indicate a null value in one of the fields
return 0
}
0 votes
MAG-II
Contributor
August 30, 2019

Ah right I was doing that, but it did not appear to be adding up. After a long time of testing / trying to figure out what the problem was I realized that there was some kind of caching issue. It is working now. Thank you for you help.

Suggest an answer

Log in or Sign up to answer