Forums

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

Setting option list for parent of cascading field based on a single select field

Maria DePasquale
Contributor
January 9, 2023

We need a three level cascading field.  Since two levels is supported (out-of-the-box), we are using a single-select field plus a cascading field.  When I select a value in the single-select, I want to customize (filter) the parent list in the cascading field.  This question has been asked before and I am attempting to use the code that was suggested.  I am unable to get it to work.  We have Jira server... not sure if that will make a difference for the code I'm using.  Also, I can make this functionality work with three single-select fields.  The complication for me is getting this to work on a cascading field, which is a requirement of the change. 

My setup:

Single-select field: Discipline  

Cascading field: Division/Area (ID: 21506)

I have a behaviour set up for the field Discipline. (I also tried this with the field set to Division/Area - also didn't work)

I have verified that the parents I'm trying to set in the option list are properly set up in the cascading field.

Here is my exact code:

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 discipline = getFieldById(getFieldChanged())
def divarea = getFieldById("21506")

def customField = customFieldManager.getCustomFieldObject("21506")
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(discipline.getValue().equals("Infra"))
{
ParticipationTypeMap += options.findAll {it.value in ['Base1', 'Infra']}.collectEntries {[(it.optionId.toString()): it.value]}

}
else if(discipline.getValue().equals("Tech"))
{
ParticipationTypeMap += options.findAll{it.value in ['Base2', 'Tech2']}.collectEntries {[(it.optionId.toString()): it.value]}
}

divarea.setFieldOptions(ParticipationTypeMap);

 

1 answer

1 accepted

0 votes
Answer accepted
Maria DePasquale
Contributor
January 9, 2023

Well I solved my own problem.  I'm posting working code in case anyone else needs it:

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 disc = getFieldByName("Discipline")

def discVal = disc.getValue() as String

def cascSelectListName = 'Division/Area'

def selectField = getFieldByName(cascSelectListName)

def selectCustomField = customFieldManager.getCustomFieldObject(selectField.fieldId)

def selectConfig = selectCustomField.getRelevantConfig(issueContext)

def selectOptions = ComponentAccessor.optionsManager.getOptions(selectConfig)

if(discVal == ("Infra))

{

    def selectAvailableOptions = selectOptions.findAll { it.value in ["Val1", "Val2"] }

selectField.setFieldOptions(selectAvailableOptions) 

}

else if (discVal == ("Tech"))

{

   def selectAvailableOptions = selectOptions.findAll { it.value in ["Val3", "Val4"] }

    selectField.setFieldOptions(selectAvailableOptions) 

}

Suggest an answer

Log in or Sign up to answer