Make cascading list mandatory based on another cascading list

Drishti Maharaj September 29, 2022

Hi,

I think this can be achieved with behaviors but I am struggling with the code.

I am trying to make "Cascading list 2" mandatory when an option is picked from "Cascading list 1"

Eg:

- On "Cascading list 1" if a user picks option "A" then they have to also fill out "Cascading list 2"

- If they pick option "B" on "Cascading list 1" then "Cascading list 2" is not required.

 

This is some of the code I was playing around with:

 

def fieldA = getFieldByName('BI Reporting & Analytics Request Categories') //this is cascading list 1

def fieldC = getFieldByName('Reporting') //this is the cascading list 2

 

def fieldAValuesThatTriggerFieldCRequired = ['Reporting'] //this is the option choosen in cascading list 1

def valueA = fieldA.value

 

def fieldCIsRequired = valueA in fieldAValuesThatTriggerFieldCRequired

fieldC.setRequired(fieldCIsRequired)

Any assistance is appreciated.

Thanks.

1 answer

0 votes
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 10, 2022

You weren't too far off.

Cascading selects return an ArrayList or String values for the selected option. The first item in the list is the first element in the cascade, and the second item in the list would be the second element.

But with behaviour, you can only fire a script when the first element changes. Changing the second element doesn't fire the script. So that second element will only ever be available in scripts triggered from other fields after you've selected both elements of the cascade.

So because the value is a list, you have to extract the first element (the only one with an available value)

Try this version:

def fieldA = getFieldByName('BI Reporting & Analytics Request Categories') //this is cascading list 1
def fieldC = getFieldByName('Reporting') //this is the cascading list 2

def fieldAValuesThatTriggerFieldCRequired = ['Reporting'] //this is the option choosen in cascading list 1

def fieldCIsRequired = false

if(fieldA.value){
fieldCIsRequired = (fieldA.value as List<String>).first() in fieldAValuesThatTriggerFieldCRequired
}
fieldC.setRequired(fieldCIsRequired)

I added a check to make sure a value was selected

Suggest an answer

Log in or Sign up to answer