Hide options in cascade select field - Behaviour Script Runner

G Krithica July 30, 2019

Hi,

 

How to hide few options from cascade field (parent option) based on issue type.

I am using the below script.

 

import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.customfields.manager.OptionsManager;

def reqtype = getFieldById("customfield_A")
def job = getFieldById("customfield_B")
def customField = customFieldManager.getCustomFieldObject("customfield_B")
def config = customField.getRelevantConfig(getIssueContext())
OptionsManager optionsManager = ComponentAccessor.getOptionsManager();
def options = optionsManager.getOptions(config)
// Set the values inside the select list field
//def ParticipationTypeMap = [null: "None"]
def ParticipationTypeMap = [:]
ParticipationTypeMap.put("-1", "None")
if(reqtype.getValue() == "1")
{
job.setFieldOptions(options.findAll {it.value in ['value1', 'VALUE 2']})

}
else if(reqtype.getValue() == "2")
{

ParticipationTypeMap += options.findAll{it.value in ['value3', 'value4']}.collectEntries {[(it.optionId.toString()): it.value]
}

job.setFieldOptions(ParticipationTypeMap);

}

 

Please let me know how to hide an option from the field.

 

Thanks,

Krithica

1 answer

1 accepted

3 votes
Answer accepted
Tom _Automation Consultants_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
July 30, 2019

Hi Krithica,

Here is a snippet of the code that I have used previously with Behaviours:

List<String> optionsA = ["ValueA", "ValueB"]
def cf1 = getFieldByName("CustomField_A")

def customField = getCustomFieldManager().getCustomFieldObjectByName("CustomField_A")
OptionsManager optionsManager = ComponentAccessor.getOptionsManager()
FieldConfig config = customField.getRelevantConfig(getIssueContext())
Options options = optionsManager.getOptions(config)
cf1.setFieldOptions(options.findAll { it.value in optionsA })

If you wanted to keep the None value when setting the options for a customfield then put the string "None" in your list of options. 

G Krithica July 30, 2019

This code is not working for me. I want to set parent field options for cascade select list.

Tom _Automation Consultants_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
July 30, 2019

Apologies that was for a Standard select list not cascading.

I have made some changes to your code, please select the field 'reqtype' in the Behaviours section, and add this server side script:

 

import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.customfields.manager.OptionsManager;

def reqtype = getFieldById(getFieldChanged())
def job = getFieldById("customfield_B")

def customField = customFieldManager.getCustomFieldObject("customfield_B")
def config = customField.getRelevantConfig(getIssueContext())
OptionsManager optionsManager = ComponentAccessor.getOptionsManager();
def options = optionsManager.getOptions(config)

// Set the values inside the select list field
def ParticipationTypeMap = [:]
ParticipationTypeMap.put("-1", "None")

if(reqtype.getValue().equals("1"))
{
ParticipationTypeMap += options.findAll {it.value in ['value1', 'VALUE 2']}.collectEntries {[it.optionId.toString()): it.value]}

}
else if(reqtype.getValue().equals("2"))
{
ParticipationTypeMap += options.findAll{it.value in ['value3', 'value4']}.collectEntries {[(it.optionId.toString()): it.value]}
}

job.setFieldOptions(ParticipationTypeMap);

}

 

Like # people like this
G Krithica July 30, 2019

Thanks so much it works.

Carlos David October 14, 2019

How would I hide options in the *child* list?

nihcet May 24, 2020

Hi Carlos,

Did you find a solution for this?

I want to hide values for child options me too.

Carlos David May 25, 2020

It is not currently possible to restrain the child list of a cascading list through groovy - in the end we used two separate select lists (i.e. non-cascading).

A Script Runner behaviour (triggered when the parent list is updated) was used to constrain the child list - switch on the different parent options using an IF/case statement & then you can set the desired list options on the other (child) select list.

-> you will need to consider what you want to display when the form first loads. Should the child list be empty until the user selects an option in the parent list? If so, this can again be achieved through a Behaviour using the 'Initialiser' property 

nihcet May 25, 2020

Thanks Carlos for info.

Peter Newton April 6, 2022

It is working for me as well. There are 2 coding errors, here is the same code corrected

import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.customfields.manager.OptionsManager;

def reqtype = getFieldById(getFieldChanged())
def job = getFieldById("customfield_B")

def customField = customFieldManager.getCustomFieldObject("customfield_B")
def config = customField.getRelevantConfig(getIssueContext())
OptionsManager optionsManager = ComponentAccessor.getOptionsManager();
def options = optionsManager.getOptions(config)

// Set the values inside the select list field
def ParticipationTypeMap = [:]
ParticipationTypeMap.put("-1", "None")

if(reqtype.getValue().equals("1"))
{
ParticipationTypeMap += options.findAll {it.value in ['value1', 'VALUE 2']}.collectEntries {[(it.optionId.toString()): it.value]}

}
else if(reqtype.getValue().equals("2"))
{
ParticipationTypeMap += options.findAll{it.value in ['value3', 'value4']}.collectEntries {[(it.optionId.toString()): it.value]}
}

job.setFieldOptions(ParticipationTypeMap);

Salwadi Edukondal May 2, 2024

@Peter Newton  it is working for me as well. I need it for only IT View but not for portal view. is there  any way?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events