Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.
×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?
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Ah, that makes sense. Seriously, thanks so much for your time!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I did to myself changing the searcher of the scripted filed.
Thankyou anyway!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
There's a solution with JMCF. But not without any third-party app.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This code snippet is meant for JMCF, not ScriptRunner. Mark's code will work in ScriptRunner.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Ok. Otherwise, as you could see, it's fairly simple to do with JMCF.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.