Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Count number of selections in a multiselect

Hi

I am using Adaptavist Scriptrunner

I have some listener code to calculate an issue score based on the values selected in various custom fields.  One of the calculations is to count the number of options selected in a custom multiselect field and based on the number add a certain value to the score field.

I don't seem to be able to find any sample code for this.  Would appreciate some assitance.

Dave

1 answer

0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 18, 2020

The options in a multiselect are held in a collection, so you can actually just use size() to get it!

def myCf = customFieldManager.getCustomFieldObjects(issue).find { it.name == "My field name" }

def fieldContent = issue.getCustomFieldValue(myCf)
def numberOfSelections = fieldContent.size()

Hi Nic

 

Sorry - I only saw your response now.

If I try your code I get an syntax error on fieldContent.size().  Please see attached screenshot.  

what am I missing?

 

Regards

 

Dave Cuff

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 19, 2020

Hmm.  Could you try forcing the type by replacing the "def" on line 73 with "Collection" - this should stop the weak-typing that Groovy claims is useful, and force the variable to be seen as the type that it really is always going to be.

(I've come to hate languages that isn't strongly-typed or minimally typed - so... many... stupid... bugs...)

Looks like we have a challenge.  Error in below screenshot.  NB we are on JIRA 7.13.5 and Scriptunner 6.0.0.  Not sure if this could be causing the error.

I have the following import commands.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.customfields.option.LazyLoadedOptionAnnotation (3).png

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 20, 2020

Try Collection,  not collection.

Ooops - sorry about that.

I did and the error has now changed.  Please see below.

Annotation (4).png

hello,

Were you able to solve the problem?

Thank you :)

I get the same error as above.  Was this ever resolved?

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Jun 16, 2023

Sorry I didn't pick this up 3 years ago.

The problem in the last post was now that the script can't know what object type the custom field is going to return.

Changing the forced type (which Java rightly insists on), to a variable one (which groovy allows for, making it much harder to write useful and clear code) will solve the problem.  Try "def fieldContent =" instead of "Collection fieldContent".

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events