Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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

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.
Jun 08, 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}

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.
Jun 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)

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

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? 

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

Thankyou anyway!

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)

@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

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

Thanks Mark Markov

Is there any solution without script runner?

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

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

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

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

Hi @David Fischer _Appfire_, 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 _Appfire_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Jun 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