Forums

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

Scriptrunner Calculated Field using custom fields

Steve Laycock
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
April 4, 2019

Hi All,

I'm completely new to scripting and I don't even know where to start and hoping someone out there can help me out.

I'm using JIRA Service desk and want to create some calculated fields based on some custom fields I've created.

Effectively I want the following calulation:
(Amount One-Amount Two)/Amount Two

I then need this converting to a percentage.

The biggest issue I have is that I dont know how to format the script or anything.

I'm pretty good at figuring amendments out if I had a base to work with but all the examples I've found online give me bits of code that don't seem compatible.

I hope this all makes sense and someone can help me out.

Thanks in advance

2 answers

1 accepted

2 votes
Answer accepted
Ben Poulson
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.
April 5, 2019

For reference, Scriptrunner uses a language called Groovy, which is an offshoot of another, far more popular language called Java, but it is mostly the same syntax (the syntax is somewhere between javascript and python, if you're familiar with those) and  works exactly the same under the hood. This is probably why it was tough to find examples of this online. For reference, I am assuming that your variables are set up similar to this:

def amtOne = 50
def amtTwo = 15

I am assuming that the variables are created with the def keyword, and are whole numbers. This is one of the syntax differences with Java that I mentioned earlier. The Java equivalent of this would be:

int amtOne = 50;
int amtTwo = 15;

The first difference is that Groovy does not require semicolons to end lines (though you can still use them). The second, more important difference is the use of def vs int. Groovy is generally very forgiving with its syntax, where Java is more strict. In groovy, the def keyword can be used to declare any variable, of any type. If the number is a whole number when you declare it as a def, it is created as an int (short for integer) variable type. This represents a 32 bit whole number (minimum value of -(2^16), maximum value of (2^16)-1). When you divide an int by an int in java, you get an int, no rounding- it uses truncation instead (cutting off everything after the decimal point, so if you divide 50 by 15 and both are ints, you get 3, not 3.3333...). To get the real value in java, you would have to use something called casting, which is when you temporarily treat a variable of one type as another type, either a double or float (both of which support decimals). It would look like this:

double result = (double)amtOne/(double)amtTwo

The value of result would be 3.333 repeating. However, groovy handles all of the casting for you, so to get the value of that equation it would look like:

def amtOne = 50
def amtTwo = 15
def result = (amtOne-amtTwo)/amtTwo

The value of result would be 2.3333.... repeating.  However, it still isn't a percentage yet. To get the raw value as a percent:

def percent = result * 100

This would be a mathematically accurate number suitable for further calculations, but I'm assuming you want to display it in a field to users rather than use the raw number, and the above would not be formatted well for that purpose. To format this number as a string (another variable type, referring to text), you could do the following.

 

First, add this as the very first line of the script:

import java.lang.Math

Then add this as the very last line:

return (Math.floor(percent*100)/100 + "%")

The details of this don't really matter, since there are tons of ways to round to two decimal places. This just multiplies by 100, removes the decimal, than divides by 100, thereby giving you two decimals and adds the percent sign. return is the keyword you use to end the script and send what immediately follows it to whatever called the script in the first place, in this case the custom field. Kind of a long winded explanation, but I feel like it's important to know why and how it works the way it does. Let me know if I left anything out!

0 votes
Alexander Bondarev
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.
April 4, 2019

Hi @Steve Laycock !

ScriptRunner has a lot of useful docs. 

Look throught this article.(Getting Started)

Alexander Bondarev
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.
April 4, 2019

also the have an excellent support team, they can help you in your case!

Like Dillon likes this

Suggest an answer

Log in or Sign up to answer