I have a multi-select custom field called "Teams Involved" with a long list of field value options. Users want a way to quickly see the number of teams involved on an issue to aid in prioritization and reporting.
Is there a way to set up a scripted field that returns the numbered "count" of all the *selected* options of this specific custom field on an issue?
Our organization uses Scriptrunner and we are on Jira Server v7.13.5
Thanks so much!
Andrew
Yes.
Add a scripted field in the custom field list as usual. You'll need to change the "searcher" to one of the number searchers if you want to search and/or sort on this.
Then go to the scripted fields section in Scriptrunner admin, find the field, set the "template" to numeric, and just add
import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.customFieldManager
def customField = customFieldManager.getCustomFieldObjects(issue)?.find { it.id == "<your multi select custom field id>" }
return issue.getCustomFieldValue(customField).size()
Note that this is from memory, and it might give you a static code check on the last line, you may need to "cast" the return to another type (try adding (long) after the "return"), and I've said <your custom field> which will be the 5 digit number for the field, which is a bit blunt. But it should work
Awesome; thanks for the notes, Nic. I'll try this out in a Staging instance ASAP and report back. The script I had was returning a "Cannot find matching method java.lang.Object#size()" error and I couldn't figure it out. Plus, this script you've provided is way cleaner.
I'll follow up with any questions soon.
Cheers,
Andrew
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hey, @Nic Brough -Adaptavist-,
Finally got around to testing this:
import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.customFieldManager
def customField = customFieldManager.getCustomFieldObjects(issue)?.find { it.id == "12345" }
return(long) issue.getCustomFieldValue(customField).size()
I did indeed see the static type checking debug error appear. So I tried adding (long) after "return" as posted in the block above, but it's still not working. I see "Please check if the declared type is correct and if the method exists."
Also shown above, where I have "12345" that's "<my custom field id>" for the multi-select custom field of which I want to return the number of selected value options.
The new scripted field uses a Number Searcher and Number Field Template.
Let me know if you have any other ideas, or notice something I input wrong - and thanks a ton for your help!
Cheers,
Andrew
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Bother, sorry, maybe it was "double" rather than "long"?
Best way to debug this is use the "preview" in the script editor. Find an issue that has two or more values for your custom field to use as the example, noting its key, then go edit the script. At the bottom under the script editing box, there's a preview where you can run the script for just that one issue (last time I used it, the key was case sensitive).
You can edit code, belt preview and see the results and, possibly more usefully, the log of what it did in the tab behind the result!
This means you can insert logging, or, more simply, return other things and see the results without having to edit and reindex all the time.
Some quick and dirty tests might be
return customField (you should be able to recognise if it is the right custom field)
return issue.getCustomFieldValue(customField) (to see if it contains a collection of the right size)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Cool! Thanks, Nic. I'll give it a go.
Cheers,
Andrew
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Did you ever get to work? I keep getting an error with the ".size()" method.
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.