Forums

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

How do I set up a field that returns count of selected value options for a custom field?

Andrew Zimmerman
Community Champion
July 25, 2019

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

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.
July 25, 2019

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

Andrew Zimmerman
Community Champion
July 25, 2019

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

Andrew Zimmerman
Community Champion
August 22, 2019

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

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.
September 19, 2019

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)

Like Andrew Zimmerman likes this
Andrew Zimmerman
Community Champion
September 19, 2019

Cool! Thanks, Nic. I'll give it a go.

 

Cheers,

Andrew

Inayat Nahvi
Contributor
June 16, 2023

Did you ever get to work?  I keep getting an error with the ".size()" method.

Suggest an answer

Log in or Sign up to answer