How to limit the number of choice in multiple select list ?

Yoann LEGRAND April 10, 2019

Hi Community !

Use case  : In Multiple select list, we need to block the creation of issue if the user selected more of 3 values.

We have Scriptrunner, so I think I can to do with a validator at the create like : 

Num = getCustomFieldValue (Myselectlist).size()
If (Num>3) {invalidexception ...etc.... }

But size() seems to be not OK.

Do you have another idea to do that ? with Behaiviour may be ?

 

thanks in advance, 

1 answer

1 accepted

1 vote
Answer accepted
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 10, 2019

I see no reason why this would not work as a validator.

You may want to protect against zero values being selected. In this case, getCustomFieldValue will be null and size() then definitely won't work.

But have you tried a simple scripted validator implementation so you can leverage the cfValues map

2019-04-10 22_42_27-Window.png

Notice the ? before size(). This will make sure that size won't fail even if the cf value is null.

If you really prefer the full custom validator, then something like this perhaps:

import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException

def myMultiSelect = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("my custom field")
def numValues = issue.getCustomFieldValue(myMultiSelect).value?.size() ?: 0
if(numValues >3 ){
throw new InvalidInputException(myMultiSelect.hiddenFieldId, "You may not select more than 3 values")
}
Yoann LEGRAND April 11, 2019

Thx Peter, It works !

Dustin Glienke September 22, 2020

Hey Peter,

why do I get an error when using your code?

2020-09-22 12_49_06-Window.png

Harsh
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.
March 12, 2024

Hi @Peter-Dave Sheehan 

The above condition shows error. As well as the script. The error: No matching method found. Could you help with this.

I want to achieve the same on create and edit screen. Can the same be achieved by Behavior?

Many Thanks

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 12, 2024

It will be easier to diagnose your error if you share your full script and the exact location of the error.

And yes, it is possible to achieve the same requirement with behaviour. However, workflow validators are more robust because they will also work in the case of API-triggered transitions.

Behaviours provides more immediate feedback to the user and can validate during edits.

Harsh
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.
March 14, 2024

Hi @Peter-Dave Sheehan 

Thank you for your response. 

I'm getting the same error which is mentioned by @Dustin Glienke . 

 

As for the script which you mentioned in your initial response above

import com.atlassian.jira.component.ComponentAccessor

import com.opensymphony.workflow.InvalidInputException

 

def myMultiSelect = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("my custom field") 

def numValues = issue.getCustomFieldValue(myMultiSelect).value?.size() ?: 0

if(numValues >3 ){

    throw new InvalidInputException(myMultiSelect.hiddenFieldId, "You may not select more than 3 values")

}

I'm getting the error at def numvalues = def numValues = issue.getCustomFieldValue(myMultiSelect).value?.size() ?: 0

 

Error is :: no such method found. 

 

Thanks..

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 14, 2024

Is the code running? Not all errors in a script editor will prevent the execution. 

Sometimes, those errors are there only because the editor can't determine the type of a value.

You can try something like this to force the error away:

import com.atlassian.jira.issue.customfields.option.Option
import com.opensymphony.workflow.InvalidInputException

def selectedOptions = issue.getCustomFieldValue("my custom field") as List<Option> ?: []
def numValues = selectedOptions.size()

if(numValues >3 ){
throw new InvalidInputException(myMultiSelect.hiddenFieldId, "You may not select more than 3 values")
}

 

Suggest an answer

Log in or Sign up to answer