Forums

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

Summation of three numerical fields to 1 new field

Jeffrey Cardona December 11, 2017

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

1 vote
Answer accepted
Daniel Yelamos [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.
December 12, 2017

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

Jeffrey Cardona December 13, 2017

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

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.
December 13, 2017

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

Jeffrey Cardona December 13, 2017

Hello Nic,

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

Jeffrey Cardona December 13, 2017

2017-12-13_14-17-12.png

Jeffrey Cardona December 13, 2017

.

Jeffrey Cardona December 13, 2017

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?

Daniel Yelamos [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.
December 14, 2017

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

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.
December 14, 2017

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

Jeffrey Cardona December 14, 2017

.

Jeffrey Cardona December 14, 2017

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

Jeffrey Cardona December 14, 2017

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

Jeffrey Cardona December 14, 2017

@Daniel Yelamos [Adaptavist] and @Nic Brough -Adaptavist- Thank you for all your help!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events