It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

0 votes

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

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

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

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

 

Cheers,

Andrew

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in Jira

Demo Den Ep. 7: New Jira Cloud Reports

Learn how to use two new reports for next-gen projects in Jira Cloud:  Cumulative flow diagram and Sprint burndown chart. Ivan Teong, Product Manager, Jira Software, demos the Cumulative ...

308 views 1 3
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you