Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Reset field options based on other field

Mario Stoilov June 28, 2019

I have to fields on a form: "Account type" and "Management preference". Both are single-select lists. I'm trying to limit the options of "Account type" based on the selection of "Management preference". Basically if the "Management preference" is "Unmanaged", then there should be only 2 options available, otherwise, you could choose anything from "Account type".

 

This is the script I wrote

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.manager.OptionsManager

def managamenetPreferenceField = getFieldById(getFieldChanged())
def accountTypeField = getFieldById("customfield_13802")

def optionsManager = ComponentAccessor.getComponent(OptionsManager)
def accountTypecustomField = customFieldManager.getCustomFieldObject("customfield_13802")
def config = accountTypecustomField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)


def managementPreference = managamenetPreferenceField.getValue()
if (managamenetPreferenceField != null) {
if (managementPreference == "Unmanaged") {
def unamanagedOptions = ['Playground - Individual', 'Playground - Shared']
accountTypeField.setFieldOptions(options.findAll {it.value in unamanagedOptions})
if (!(accountTypeField.getValue() in unamanagedOptions)) {
accountTypeField.setFormValue('Playground - Individual')
}
} else {
accountTypeField.setFieldOptions(options)
}
}


The problem comes, that the "optionsManager.getOptions(config)" methd returns even the disabled options, which is not ok. Is there a way (through the code) I could filter out the enabled options?

2 answers

1 accepted

1 vote
Answer accepted
PD Sheehan
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.
June 28, 2019

You could just change this:

def options = optionsManager.getOptions(config).findAll{!it.disabled}
Kian Stack Mumo Systems
Community Champion
June 28, 2019

You beat me to it Peter, but that was what I found as well!

0 votes
Kian Stack Mumo Systems
Community Champion
June 28, 2019

I was able to find them doing something like this:

options.findAll {it.getDisabled() == false}

Suggest an answer

Log in or Sign up to answer