Forums

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

Show select list custom field value based on other Select list field value Script Runner - Behaviour

durga_panda January 31, 2021

Hi Guys,

I am not a coder so I need some help here.

We are on Jira 8.5.x. We have a requirement. There are two select list custom fields. 
Suppose Field1and Field2.

If I am selecting "A" from Field1 then Field2 should show these options: "Value1", "Value2" 
And if I am selecting "B" from Field1 then Field2 should show: "Value3", "Value4"

I tried and created the below code but it is not working.


"

import com.atlassian.jira.component.ComponentAccessor
import static com.atlassian.jira.issue.IssueFieldConstants.RESOLUTION
def customFieldManager = ComponentAccessor.customFieldManager
def optionsManager = ComponentAccessor.optionsManager
def Field2 = getFieldById("customfield_10500")
def Field1 = getFieldById("fieldchanged")

def Field2Value = customFieldManager.getCustomFieldObject("customfield_10500")
def Field1Value = Field1.getValue()
def options = optionsManager.getOptions(Field2Value)[0]

if (Field1.value == "UC") {
def allowedField2 = options.findAll { it.value in ["Value1", "Value2"] }
Field2Value.setFieldOptions(allowedField2)
} else if ((Field1.value == "DevOps")) {
def allowedField2 = options.findAll { it.value in ["Value3", "Value4"] }
Field2Value.setFieldOptions(allowedField2)
}

"

2 answers

1 accepted

0 votes
Answer accepted
Lih Yao Tiong
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.
February 2, 2021

Hi @durga_panda 

I applied your script to my local environment but there are some error messages, and I didn’t check the reason causing them.

Here's my sample script that should fulfill your requirement:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager

def Field1 = getFieldById("customfield_10200") // parent select list 1
def Field2 = getFieldById("customfield_10500")// child select list 2 (Depending Field1's value)

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

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

Map fieldOptions1 = [:]
fieldOptions1.put ("-1", "None")
fieldOptions1.putAll (["1":"A1","2":"A2"])

Map fieldOptions2 = [:]
fieldOptions2.put ("-1", "None")
fieldOptions2.putAll (["4":"B1","5":"B2"])


String Value1 = (String) Field1.getValue() //check the parent select list value

if (Value1 == "A") //if the value is A
{
Field2.setFieldOptions(fieldOptions1)
}
else if (Value1 == "B") //if the value is B
{ Field2.setFieldOptions(fieldOptions2)
}

 

 Could you please give it a try and let me know if it works for you?

durga_panda February 2, 2021

HI @Lih Yao Tiong 

Thank you for this code. This code did not gave me any error however it is not working for me.

maybe I am doing something wrong.

Map fieldOptions1 = [:]
fieldOptions1.put ("-1", "None")
fieldOptions1.putAll (["1":"A1","2":"A2"])

can you please help me if i am understanding below correctly? is it
fieldOptions1.putAll (["Option_ID1":"Option_value1","Option_ID2":"Option_Value2"])


Lih Yao Tiong
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.
February 3, 2021

Hi @durga_panda

Glad to know it works for you now:) Yes, it's just like the ID, key for the option's value.

I hope it resolves your queries.

Could you please mark it as an accepted answer, please?

It might be helping those looking for the solution.

0 votes
durga_panda February 3, 2021

I was actually pasing this into initliazer, instead code on field behaviour worked. Below is my version of code which is working too

 

import com.atlassian.jira.component.ComponentAccessor;
import com.onresolve.jira.groovy.user.FieldBehaviours;
import groovy.transform.BaseScript;
@BaseScript FieldBehaviours fieldBehaviours;

def customFieldManager = ComponentAccessor.getCustomFieldManager();
def optionsManager = ComponentAccessor.getOptionsManager();
def Field2 = customFieldManager.getCustomFieldObject("customfield_10500")
def Field2_config = Field2.getRelevantConfig(getIssueContext());
def Field2_options = optionsManager.getOptions(Field2_config);
def Field2field = getFieldById("customfield_11111");
def Field1field = getFieldById("customfield_22222");
def Field1Value = Field1field.getValue()

def AllowedValue = [ ]

if (Field1Value == "Value1"){
AllowedValue = ["X1", "X2", "X3"]
} else if (Field1Value == "Value2"){
AllowedValue = ["A1", "A2", "A3"]

Field2field.setFieldOptions(Field2_options.findAll{it.value in (AllowedValue)})
Vinay Humility July 18, 2024

Does the same code works for Jira cloud

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, jira product discovery, jpd premium, product management, idea management, product discovery, jira premium, product planning, atlassian community, product development, roadmap planning, product prioritization, feature management

Introducing Jira Product Discovery Premium ✨

Jira Product Discovery Premium is now available! Get more visibility, control, and support to build products at scale.

Learn more
AUG Leaders

Atlassian Community Events