ScriptRunner behavior show/hide custom field based on cascading select and single select fields

vcatl2020 April 21, 2021

I'm trying to display a custom number field and required only on two combinations, otherwise the intent is to hide a custom number field on a specific issue type using Behaviors in ScriptRunner but no luck to get this to work. The two combinations are cascading select option values (parent - Test Support and child - Prod Environment) along with a singleselectField (Admin, Admin and Developer, EndUser and null).

Please review my code for this requirement effort and kindly suggest on the code change as needed to get this to work. This code seems to fail mainly due to Cascading select field validation and for the sake of testing, when I removed the code with reference to Cascading select field then it works fine. Your quick turnaround with code suggestions will be of great help.

 

def cascadecustomField = getFieldByName("What do you want to select?")

def selectedOptions = cascadecustomField.getValue() as Map

def parentOptionLevel = null
def childOptionLevel = '1'

def ParentOption = selectedOptions.get(parentOptionLevel).toString()
def ChildParentOption = selectedOptions.get(childOptionLevel).toString()

def numberField = getFieldByName("ID Number")
def singleselectField = getFieldById(getFieldChanged())

def selectedOption = singleselectField.getValue() as String

if(ParentOption == "Test Support" && ChildParentOption == "Prod Environment"){

if(selectedOption == "Admin" || "Admin and Developer"){
numberField.setHidden(false)
numberField.setRequired(true)
}

if(selectedOption == null){
numberField.setHidden(true)
}


if(selectedOption == "EndUser"){
numberField.setHidden(false)
numberField.setRequired(false)
}
}

1 answer

0 votes
Max Lim _Adaptavist_
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.
April 25, 2021

The getFieldByName() on cascading custom field returns an ArrayList.

Therefore, to access the cascading custom field values in Behaviour, you can just use:

def parentOption = getFieldById(getFieldChanged()).getValue()[0]
def childParentOption = getFieldById(getFieldChanged()).getValue()[1]

 I hope this helps!

vcatl2020 July 7, 2021

Lost myself in another world of Atlassian work and now back onto this again, sorry to respond this late @Max Lim _Adaptavist_ and unfortunately that did not worked. I revised my code and it is kind of working but only after updating the ticket and what I was expecting is to work instantly prior to update.

Do I need to tweak any in the following revised code to make this to work as expected dynamically or on the fly?

def cascField = getFieldByName("Please select a category")

def hiddenField = getFieldByName("Text field")

def value = cascField.getValue() as List

def cfSelect = getFieldByName("Single select field")

def selectedOption = cfSelect.getValue() as String

if (value.size() == 2 && value.get(1) == "cascfieldValue" && selectedOption == "option1") {
hiddenField.setHidden(true)
}
else
hiddenField.setHidden(false)

vcatl2020 July 13, 2021

Any inputs or thoughts on how to perform the above code validation specifically for the cascading select field please?

getFieldById(getFieldChanged()) doesn't seems like working in my code with reference to show/hide field...

vcatl2020 July 28, 2021

We use Dynamic forms Cascade select custom field and Behaviors are not currently compatible with Dynamic Forms. Product support adaptavist confirmed having an existing improvement ticket here: SRJIRA-3401. Resting this post finally!

Suggest an answer

Log in or Sign up to answer