Behavior hides (none) option - how could it show

Mihai Mihai July 12, 2019

Hello,

 

We have a Behavior (scriptrunner) that shows only certain values in a select list field when certain other values are chosen in a different select list field:

 

Here is the script:

 

def RespProject = getFieldByName("Responsible Project")
def AgileTeam = getFieldByName("Agile Team")

def customField = customFieldManager.getCustomFieldObject(AgileTeam.getFieldId())
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)

if (RespProject.getValue() as String == "FC Audio") {

def optionsMap = options.findAll {
it.value in ["VSP", "VST", "Aud_Decibels", "None"] // list of options you want to show
}.collectEntries {
[
(it.optionId.toString()): it.value
]
}
AgileTeam.setFieldOptions(optionsMap)
}

else if (RespProject.getValue() as String == "FC Connectivity") {

def optionsMap = options.findAll {
it.value in ["DCON_Fermi", "None"] // list of options you want to show
}.collectEntries {
[
(it.optionId.toString()): it.value
]
}
AgileTeam.setFieldOptions(optionsMap)
}

 

 

However, I cannot make it still allow the selection of the "None" option. It automatically selects the first value in the list, and sometimes we just want to have a value selected in "Responsible project", but the "Agile Team" can still be empty, until an analysis is made.

 

Thank you!

1 answer

1 accepted

3 votes
Answer accepted
Ilya Turov
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 12, 2019

"None" is not really an "option", but if you add either [null: "None"] or  ["-1": "None"] to your optionsMap it should work. Basically, try

AgileTeam.setFieldOptions(optionsMap +  ["-1": "None"] )
Payne
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 12, 2019

I do a similar thing in a Behaviour and add a key/value pair of empty strings, i.e. ["":""]

Like Ilya Turov likes this
Mihai Mihai July 12, 2019

Thank you very much, it works!

DH September 9, 2020

For me, on Jira 7.9.2 and Scriptrunner 6.3.0, I got a 'static type checking' error by following the above example. 

I solved the error by type-casting the added Option, like so:

accountType.setFieldOptions(selectAvailableOptions + (Option) ["-1": "None"])

Added import statement: 

      import com.atlassian.jira.issue.customfields.option.Option

klosote April 9, 2021

Hi, I tested all your options specially the (Option) ["-1": "None"] and i got the following error on JIRA 8.13:

Is there any other way to do it?

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '{-1=None}' with class 'java.util.LinkedHashMap' to class 'com.atlassian.jira.issue.customfields.option.Option' due to: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: com.atlassian.jira.issue.customfields.option.Option(LinkedHashMap)

Suggest an answer

Log in or Sign up to answer