Show/Hide options from cascading select list

Hi,

I need to add/remove options from cascading select list while creating a new issue based on the value selected in another field.

I think that the best way is to use Behaviour plugin => I will set the basic options that have to be shown everytime and then, based on the selection of another field, I add the additional options related to the specific selection.

 

Can anyone suggest me a way to implement this behaviour?

 

Thank you in advance

Regards

Arianna

3 answers

1 accepted

This widget could not be displayed.

Hi Ariana,

I have enclosed a code sample below which shows how you can use the Behaviours plugin provided by script runner to dynamically remove and set from a Select List field based on an option selected inside a check box field.

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()

// Get a pointer to my select list field
def selectList = getFieldByName("Demo Select List")

// Get a pointer to my check box field
FormField checkBox = getFieldByName("Check Box 1")

// Get access to the required custom field and options managers
def customField = customFieldManager.getCustomFieldObject(selectList.getFieldId())
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)


// Get the checkbox option value as a String
def checkBoxVal = checkBox.getValue()

// Logic to do some actions depending on what check box value is selected
if (checkBoxVal == "A") {
    // define and set the required options when checkbox A is selected
    def optionsMap = options.findAll {
        it.value in ["A", "B"] // list of options you want to show
    }.collectEntries {
        [
                (it.optionId.toString()): it.value
        ]
    }
    selectList.setFieldOptions(optionsMap)
} else if (checkBoxVal == "B") {
    // define and set the required options when checkbox A is selected
    def optionsMap = options.findAll {
        it.value in ["C", "D", "E"] // list of options you want to show
    }.collectEntries {
        [
                (it.optionId.toString()): it.value
        ]
    }
   selectList.setFieldOptions(optionsMap)
}

 

I hope this helps.

Kristian

Thank you very much!! Arianna

Hi @Kristian Walker (Adaptavist),

any news about cascading select list?

 

I'm trying this behaviour script:

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Level
import com.atlassian.jira.issue.customfields.option.Option
log.setLevel(org.apache.log4j.Level.DEBUG)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()
FormField IssueSource = getFieldById(getFieldChanged())
log.debug "Modifica rilevata: " + IssueSource
String issuesourcevalue = (String) IssueSource.getValue()
// Get a pointer to Department/Job Title select list
FormField deptJobTitleList = getFieldByName ("Department/Job Title")
def deptJobTitleSelectAA = customFieldManager.getCustomFieldObject(deptJobTitleList.getFieldId())
def deptJobTitleSelectOth = customFieldManager.getCustomFieldObject(deptJobTitleList.getFieldId())
def configAA = deptJobTitleSelectAA.getRelevantConfig(getIssueContext())
def configOth = deptJobTitleSelectOth.getRelevantConfig(getIssueContext())
def optionsAA = optionsManager.getOptions(configAA)
def optionsOth = optionsManager.getOptions(configOth)
Map<String,Object> optionsMapAA
Map<String,Object> optionsMapOth
optionsMapAA = optionsAA.findAll {
    it.value in ["None", "A", "B", "C"] 
}.collectEntries {
    [
        (it.optionId.toString()): it.value
    ]
}
log.debug "opt map: " + optionsMapAA
optionsMapOth = optionsOth.findAll {
    it.value in ["None", "D", "E", "F", "G","H","I","L","M","N","O","P"]
}.collectEntries {
    [
        (it.optionId.toString()): it.value
    ]
}
log.debug "opt map: " + optionsMapOth
// Get a pointer to Company select list
FormField Company = getFieldById("customfield_14600") 
if(issuesourcevalue != null && Company.getValue() != null){
    if (issuesourcevalue.contains("1") && Company.getValue().equals("Headquarters")){
        deptJobTitleList.setFieldOptions(optionsMapAA)  
    }else{
        deptJobTitleList.setFieldOptions(optionsMapOth) 
    }
}

When I open new issue create screen and change the value of the field to which the script is linked to, I get, only the first type, the correct result in the cascading select.

As soon as I change again the value, the first level of the cascading select is correctly changed but the second level is empty.

 

Why at the first value selection both level of the select are correctly loaded and on the following changes the second level is no longer loaded?

 

Thank you in advance for your help

 

Hi Kristian

I have created the following script based on your advice, trying to get the choice of one select list to alter the options present on another. But nothing seems to happen when I use this code. Can you possibly advise?

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()

// Point towards Change Result Reason custom field list
def selectList = getFieldById("customfield_15601")

// Point towards Change Result custom field list
def checkBox = getFieldById("customfield_15600")

// Get access to the required custom field and options managers
def customField = customFieldManager.getCustomFieldObject(selectList.getFieldId())
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)

// Get the checkbox option value as a String
def checkBoxVal = checkBox.getValue()

// Logic to do some actions depending on what check box value is selected
if (checkBoxVal == "Successful") {
// define and set the required options when checkbox A is selected
def optionsMap = options.findAll {
it.value in ["Implemented Successfully"] // list of options you want to show
}.collectEntries {
[
(it.optionId.toString()): it.value
]
}
selectList.setFieldOptions(optionsMap)
} else if (checkBoxVal == "Partially Successful") {
// define and set the required options when checkbox A is selected
def optionsMap = options.findAll {
it.value in ["Bug", "Compatibility Issue", "Configuration Issue", "Deployment Failure", "Did Not Address Reason for Change", "Human Error", "Infrastructure Failure", "Incomplete CHG", "Incomplete documentation", "Management/Business Request", "Methodology not understood or tested", "Other", "Successful", "Testing Failure"] // list of options you want to show
}.collectEntries {
[
(it.optionId.toString()): it.value
]
}
selectList.setFieldOptions(optionsMap)
} else if (checkBoxVal == "Unsuccessful") {
// define and set the required options when checkbox A is selected
def optionsMap = options.findAll {
it.value in ["Bug", "Compatibility Issue", "Configuration Issue", "Deployment Failure", "Did Not Address Reason for Change", "Human Error", "Infrastructure Failure", "Incomplete CHG", "Incomplete documentation", "Management/Business Request", "Methodology not understood or tested", "Other", "Successful", "Testing Failure"] // list of options you want to show
}.collectEntries {
[
(it.optionId.toString()): it.value
]
}
selectList.setFieldOptions(optionsMap)
}
This widget could not be displayed.

Maybe you can use javascript/behavior instead

This widget could not be displayed.

Hi Arriana,

 

I have been looking into this and it appears that setting values in Cascading Select fields is currently not fully supported.

The way to get around this would be to implement two single select lists and then use the OptionsManager() to dynamically add and remove values.  An example of how this can be done is shown in the answer located here.

Thanks


Kristian

Kristian - has this been resolved with new updates? If this works for a select list shouldn't it be easy to adapt for a cascading select? Forgive me if I'm oversimplifying.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Aug 22, 2018 in Marketplace Apps

How a Marketplace app tech team is achieving gender diversity

Hello! My name is Genevieve Blanch, and I'm the Marketing Manager at RefinedWiki, creators of apps to give teams the tools to customize Atlassian platforms. Currently, 44% of the tech team at Re...

512 views 3 18
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you