Summation of three numerical fields to 1 new field

Hello i'm trying to figure out as I'm not familiar with groovy scripts nor script runner.

I have 3 custom numerical fields named:

"An.Total", "Eng.Total", "Ops.Total"

For each issue in my current project, I'd like to add all three values into one value on the same issue called "Total".

"An.Total" + "Eng.Total" + "Ops.Total" = "Total"

 

Anything would help,

Thank you

1 answer

1 accepted

Accepted Answer
1 vote

Hi!

You need a scripted field, with a numerical searcher and template.

For the code, use this:

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf1 = customFieldManager.getCustomFieldObjectByName("An.Total")
def cf2 = customFieldManager.getCustomFieldObjectByName("Eng.Total")
def cf3 = customFieldManager.getCustomFieldObjectByName("Ops.Total")
def sum = issue.getCustomFieldValue(cf1)?:0 + issue.getCustomFieldValue(cf2)?:0 + issue.getCustomFieldValue(cf3)?:0

return sum

Make sure that your fields  are numeric, else this could cause some null exceptions.

Hope this helped, if you need any more help, please do say, and if this helped, please accept and upvote my answer so that other users can benefit from this question.

Cheers!

Dyelamos

Hi Daniel,

Does this have to be placed into the "Inline Script" section. If so, i'm seeing an error on the addition operator "+". 

Thanks,
Jeff2017-12-13_14-09-30.png

The error suggests one or more of the values coming back from the fields is not a number.

Hello Nic,

I've checked all 3 field including "An.Total","Eng.Total","Ops.Total". When created I've made them all Numerical fields.

Also, I noticed that sum is defined by 

def sum = issue.getCustomFieldValue(cf1)?:0 + issue.getCustomFieldValue(cf2)?:0 + issue.getCustomFieldValue(cf3)?:0

and is then is

return sum.

I did create a custom field named "Total", do I want to replace "sum"with my custom field "Total" and also define it in the line before?

Jeffrey!

Those errors are static type checking. You can disregard them unless they give problems in execution time.

Please just save your script as is. It should work.

Also before you try any scriptrunner scripting you should thoroughly read, at least the basic sections of our documentation.

If you check the "Concepts" section, you will see an explanation about static field errors and what they actually are.

Without reading the documentation you might experience problems that are easily solved.

Cheers
DYelamos

I'd agree with Daniel here, the static type checking is worth a read if you're going to do a lot of scripting, or run into these sort of errors.

An experiment to help see what is happening might be to try this:

def v1 = issue.getCustomFieldValue(cf1)?:0

def v2 = issue.getCustomFieldValue(cf2)?:0

def v3 = issue.getCustomFieldValue(cf3)?:0

return "Values: " + v1 + " : " + v2 + " : " + v3

That should help you see where the static types matter, and confirm what the code is getting back from the issue.

To see what is being passed around internally, it might be worth seeing:

return v1.getClass().getSimpleName() + " : " + v1.getClass().getSimpleName() + " : " + v1.getClass().getSimpleName() + " : "

Although I suspect this sort of brute-force information dump won't be needed

Hello,

I was able to finally receive a return value. The only issue is the value cf1 is only being returned and I'm receiving error now under

The values I have under this issue is 12 + 13 + 14

return sum I receive just the 12.2017-12-14_12-21-45.png

What I did to resolve this issue was since it didn't like summation operator define as

define sum = issue.getCustomFieldValue(cf1)?:0 + issue.getCustomFieldValue(cf2)?:0 + issue.getCustomFieldValue(cf3)?:0

I used the summation operation in the return clause and also returned the value as a double since it was complaining about that too and came up with this.

 

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf1 = customFieldManager.getCustomFieldObjectByName("An.Tot")
def cf2 = customFieldManager.getCustomFieldObjectByName("Eng.Tot")
def cf3 = customFieldManager.getCustomFieldObjectByName("Ops.Tot")
def sum1 = issue.getCustomFieldValue(cf1)?:0
def sum2 = issue.getCustomFieldValue(cf2)?:0
def sum3 = issue.getCustomFieldValue(cf3)?:0
return (sum1 + sum2 + sum3) as double

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Thursday in Marketplace Apps

You + one app + a desert island...

Hi all! My name is Miles and I work on the Marketplace team. We’re looking for better ways to recommend and suggest apps that are truly crowd favorites, so of course we wanted to poll the Community. ...

567 views 4 5
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you