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
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.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.