Forums

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

Caclulate Sum and display Value in Custom Field

GS1337
Contributor
June 18, 2019

Hi,

 

i found a script that calculates the sum of 5 custom fields and devides it by 5, now im struggling to actually display this Value on a custom field in the Issue. Can anybody help me with the Code ?

 

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

def issue = ComponentAccessor.getIssueManager().getIssueObject("ID-29")
def result_CF = "customfield_19921"

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
List<String> fieldNames = ["customfield_19824","customfield_19827", "customfield_19825", "customfield_19828", "customfield_19826"]
List<Integer> scores = []
for (String field : fieldNames){
def cf = customFieldManager.getCustomFieldObject(field)
scores.add((Integer) issue.getCustomFieldValue(cf)?: 0)
}
//return scores.sum {it} / 5
result_CF = scores.sum {it} / 5
return result_CF

1 answer

2 votes
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.
June 18, 2019

I would convert this to a "scripted field".

Add a new scripted field, set it with the output type "number", and although I've not tested your code, at a glance, I would expect it to work with just one modification - remove the "issue = ..." line.  In a scripted field, the object "issue" will already contain the current issue.

When you add the script to the field, look underneath the edit box, for the "preview" function.  If you put ID-29 in there (be aware in many versions of SR, it is annoyingly case-sensitive!) and press preview, you should get the calulation result.

GS1337
Contributor
June 18, 2019

Thx for the answer, yes i am aware about the issue, i just added the line with ID-29 so i can test it in the script console. So i have a field with number type, but I am still unsure what the code is to actually write the number in the field i want it to display on. Also if i take a scripted field and not a custom field, do i write the script in the scripted field? It should be in a post function on a workflow status

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.
June 18, 2019

Yeah, scripted fields have to be done in two steps - part of it is done in Jira, the rest in Scriptrunner.

The Jira part is the field definition, like any other custom field.  Add a custom field of type "scripted" to your Jira and put it on the issue view screen(s) you want to see it in.  Edit the field definition (not configure), in this case, setting the "searcher" to "number range searcher"

Now to the more complex bit in SR.  Go to SR -> Scripted fields.  You should see your new field in it.  Click through into it to get the script definition screens.  In there, there's several fields, but you're only interested in

  • Template - choose "number field"
  • Script box - paste your script in there
  • Preview section at the bottom - give it your test issue and see what it does.

In terms of the code, it's fine - the thing about a scripted field is that it takes the return value from the script.  Your script is trying to return the results of the calculation, so that should work fine, although, you might have to "cast" the value to the right type of number with something like

return (double) result_CF

The preview test will tell you if the return type is wrong.

Suggest an answer

Log in or Sign up to answer