How do I populate a custom field with the sum of multiple numeric custom field values?

Marisa Hager June 8, 2018

I have 9 custom fields where I ask the user to enter a value between 1 and 5 to indicate how highly each rates for priority.  I then need a total prioritization score that adds all 9 of the fields together for a single score and populates my "prioritization score" custom field - which users can then view in the ticket and issue list views, to sort on and export for use elsewhere. 

We DO have script runner, and I had a script that I put in the description of the total score custom field that added 2 values together successfully as a test - but after putting in the other 7 fields, it would only add the first two and never include the other 7.  So then I went back to the original script with just 2, but it wouldn't return a total at all anymore.  The field just wouldn't display.  Here's what I used:

<!-- @@Formula: (issue.get("customfield_13311") != null ? issue.get("customfield_13311") : 0) +

(issue.get("customfield_13312") != null ? issue.get("customfield_13312") : 0) -->

Calculated field made up of the scores in individual prioritization custom fields.

I've re-indexed, but nothing changed.  Is there something wrong with the script, or the system?

5 answers

1 accepted

1 vote
Answer accepted
Mark Markov
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.
June 8, 2018

Hello @Marisa Hager

I dont really understand what code via scriptrunner you put in the description, but for that case you can use Scriptrunner Scripted Fields

Add-ons -> Scripted Fields -> Add

Template Numeric (or whatever you need)

and code like this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
List<String> fieldNames = ["field1","field2","field3","field4","field5","field6","field7","field8","field9"]
List<Integer> scores = []
for (String field : fieldNames){
def cf = customFieldManager.getCustomFieldObjectByName(field)
scores.add((Integer) issue.getCustomFieldValue(cf)?: 0)
}
return scores.sum {it}
Marisa Hager June 11, 2018

Hi @Mark Markov - many thanks for your help!  But I followed your instructions and swapped in my custom field id's for the variables above, but before I could run it, it showed this error in line 11, column 8 (the last line):

[Static type checking] - Cannot return value of type java.lang.Object on method returning type java.lang.Double

Then I went in to the log and it displayed a little more info (below).  The ticket where I've populated scores for the all the individual custom fields is there and contains the "prioritization score" scripted field where I hoped to see the total: 

2018-06-11 09:50:17,456 ERROR [customfield.GroovyCustomField]: *************************************************************************************
Script field failed on issue: CLT-215, field: Prioritization Score
java.lang.NullPointerException
 at com.atlassian.jira.issue.IssueImpl.getCustomFieldValue(IssueImpl.java:896)
 at com.atlassian.jira.issue.Issue$getCustomFieldValue$2.call(Unknown Source)
 at Script88.run(Script88.groovy:9)

Any advice you have would be greatly appreciated - thanks so much for your time,

Marisa

Mark Markov
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.
June 11, 2018

You can ignore static type checking because groovy have dynamic types, and when you script will be run it will cast it correctly.

In my example you need to swap "field1","field2", etc with fieldnames, not ids.

If you want to use fields ids 

Replace

def cf = customFieldManager.getCustomFieldObjectByName(field)

on

def cf = customFieldManager.getCustomFieldObject(field)
Marisa Hager June 11, 2018

Ah, that makes sense.  Seriously, thanks so much for your time!

Chiara Squilloni March 20, 2019

Hi @Mark Markov and thank you so much. I use your solution with script runner and this work.

I have a question, I need the result to be in float. I modified your script but the result is without a comma. Maybe I'm the cast wrong. Can you help me?

Here is the script:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
List<String> fieldNames = ["field1","field2","field3","field4"]
List<Float> scores = []
for (String field : fieldNames){
def cf = customFieldManager.getCustomFieldObjectByName(field)
scores.add((Float) issue.getCustomFieldValue(cf)?: 0)
}
return scores.sum {it}

If I sum 5 + 5 + 3,5 + 6 the result is 195 and not 19,5

Can you help me? 

Chiara Squilloni March 20, 2019

I did to myself changing the searcher of the scripted filed.

Thankyou anyway!

0 votes
Amit Kumar Singh February 22, 2022

Hi @Mark Markov ,

I am also facing the same issue and getting below error.

 

java.lang.NullPointerException at com.atlassian.jira.issue.IssueImpl.getCustomFieldValue(IssueImpl.java:951) at com.atlassian.jira.issue.Issue$getCustomFieldValue$2.call(Unknown Source) at Script134.run(Script134.groovy:9)

0 votes
Venkatesh October 27, 2021

@Mark Markov 

Hi Mark Markov,

Your script is working fine for addition of positive numbers. If we add negative numbers to positive numbers it's giving wrong output. Could you please help me.

 

Thanks in Advance

Venkatesh October 27, 2021

The Script is working fine for addition of both Positive and Negative numbers.

Thanks Mark Markov

0 votes
Parag Madpuwar August 6, 2020

Is there any solution without script runner?

David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 6, 2020

There's a solution with JMCF. But not without any third-party app.

0 votes
David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 8, 2018

This code snippet is meant for JMCF, not ScriptRunner. Mark's code will work in ScriptRunner. 

Marisa Hager June 11, 2018

Hi @David Fischer, yeah I didn't realize the difference at first - a friend gave me the JMCF snippet.  I don't have a preference and would use either if I could get them to work.   Hopefully Mark can help me out with the ScriptRunner code and I won't need to fall back on the JMCF :)

David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 11, 2018

Ok. Otherwise, as you could see, it's fairly simple to do with JMCF.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events