Forums

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

Jira Datacenter: how I can sum 2 fields and show in "total"?

Deleted user February 23, 2023

In an issue I am preparing to manage an initiative, I put three custom fields as follows:
1) system time
2) programmer time hours
3) total time hours ( = system engineer time + programmer time)

On the Jira datacenter I have at my disposal at the moment, I do not have the excellent JSU plugin and I do not know if I can get it anytime soon. But I do have the ancient and outdated and difficult to use Script Runner.

Can someone please help me understand how to then with Script Runner read the values of custom fields 1 and 2 and put the sum in custom field number 3?

Thank you in advance.

 

1 answer

1 vote
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.
March 8, 2023

Do you mean the Scriptrunner that can do everything JSU can do (directly for a couple of years - you have not needed to write any scripts to replace JSU with SR any more), and a load more, and has recently been updated with HAPI (which I like to explain as "Helpful API"), making it even easier to script?

Obviously, that's a blatant advert.  I used to use JSU a lot myself and it's very good, but it is limited compared with Scriptrunner.  SR was always far more powerful and flexible.  If I take my Adaptavist hat off and think as an independent Jira admin, then I still hold that opinion - the canned scripts can easily directly replace 95% of JSU, and HAPI makes scripting the other 5% far easier.

Anyway, now I've done my obligatory marketing thing, I will try to give you an answer:

I do not know if you want to do this as a scripted field (read-only field which automatically updates), or a listener (that will push the calculation into an existing field, which people can edit if you put it on an edit screen).

The scripted field is usually the right way to do this, and the code for that is more simple than a listener (you just have to return a value, not write it to a custom field).  This script tries to be self-explanatory, but you could shorten it a bit. (More with HAPI - I think it could be a single line with HAPI)


 

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

// note the assumptions that these fields have an exact name, exist, and are number fields

final field1Name = "System Time"
final field2Name = "Programmer Time Hours"

def f1field = customFieldManager.getCustomFieldObjects(issue).find { it.name == field1Name }
def f2field = customFieldManager.getCustomFieldObjects(issue).find { it.name == field2Name }

def f1value = issue.getCustomFieldValue(f1field)
def f2value = issue.getCustomFieldValue(f2field)

return ( f1value + f2value)

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events