With Scriptrunner Behaviour - hide a field based on the values of a cascading field

anarcon GmbH June 7, 2021

Hi all,

 

I want to hide customfield_31613 when the values of the cascading field customfield_32216 are "a" and "b" (parent value and child value) respectively.

I'm using Behaviours for it, my current code:

def listField = getFieldById("customfield_32216")
def hiddenField = getFieldById("customfield_31613")

// get cascading list value (parent and child)
String listString = listField.getValue()
// if cascading list matches one of two selections, hide the Due Date OPS field
if ( listString == ["a", "b"]) {
hiddenField.setHidden(true)
}

 

It doesn't work. Can you tell me where I did wrong?

 

Thank you,

Duong

1 answer

1 accepted

0 votes
Answer accepted
Amir Mustapha _Adaptavist_ June 7, 2021

Hi @anarcon GmbH,

If you wanted to hide a field based on a cascade list value using behaviour, you could try this script to hide the field:

//get the custom field details
def customField = getFieldByName("Cascade1")
def custField = getFieldByName("Select1")
//get the cascade field value
def cfAValue = customField.value as List

//for debug purpose
log.warn "cfAValue: " + cfAValue

if (cfAValue) {

//Size at least 2 [parentValue, childValue]
if (cfAValue.size()>1) {
//The 1st value will be your parent value, and the 2nd value will be your child value:
def parentValue = cfAValue.get(0)
def childValue = cfAValue.get(1)

//For debug purpose
log.warn "cfAValue: ${cfAValue}"
log.warn "parentValue: ${parentValue}"
log.warn "childValue: ${childValue}"

if(parentValue == "A" && childValue == "B"){
custField.setHidden(true)
} else {
custField.setHidden(false)
}
}
}

 I hope this able to help you with your requirement.

anarcon GmbH June 8, 2021

Hi @Amir Mustapha _Adaptavist_ ,

 

Thank you for your answer, it worked!

One problem though: when we change the cascade field values back to empty while creating the issues, the hidden field doesn't show up again. Do you know how to fix this?

Amir Mustapha _Adaptavist_ June 8, 2021

Hi @anarcon GmbH,

From the sample script provided, if it meets the condition, you will setHidden(true) else you should also set the value to setHidden(false) from this sample condition to make the field reappear:

if(parentValue == "A" && childValue == "B"){
custField.setHidden(true)
} else {
custField.setHidden(false)
}

 I hope this able to answer.

Like Bridy Frett likes this
Bridy Frett November 30, 2021

Thank you for the sample code - it works great!

One additional question.  I notice when the create screen is initially displayed, custField is visible.  Once I select the first part of the cascading field, the custField becomes hidden.  It's not visible again until the user selects the matching criteria.

How can I make the field hidden on create ... hidden until the user selects the matching criteria to make the field hidden.

Thanks!

Bridy

Bridy Frett November 30, 2021

I figured it out right after the post, of course!

I made the following adjustment

if (cfAValue) {

   ... same code as above

} else {
custField.setHidden(true)
}

Suggest an answer

Log in or Sign up to answer