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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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?

3 answers

1 accepted

1 vote
Answer accepted

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 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!

0 votes

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

Hi @David Fischer _Innovalog_, 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 :)

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

Is there any solution without script runner?

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

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Marketplace Apps & Integrations

5 mobile apps for Jira Cloud to boost productivity

  It’s very important to have access to the workflow process from anywhere. Especially if you manage the work of others. There is no difference whether you’re out of office, or drive a ca...

260 views 2 5
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you