Hello all, I need help in setting up a validation on Cascading drop-down custom field in a JIRA Data Center environment using ScriptRunner Behaviours. How to make a "child" part mandatory, but only if a "parent" has a value?
Hhi @Jira Dev
Welcome to the Community.
I have provided the exact answer to your requirement in this Community Post.
The solution provided in the link also uses ScriptRunner's Behaviour and performs a validation to ensure that both Parent and Child options are selected in the Cascade Select List.
I hope this helps answer your question. :-)
Let me know how it goes.
Thank you and Kind regards,Ram
Not all of my “parent” options have “child” values, so the script doesn’t work the way I need it to. It currently makes the “child” field mandatory for every “parent,” even when there are no child options available.
Hi @Jira Dev
Thank you for getting back and providing your feedback.
For your case, you must modify the sample code that I provided in the Community Post to something like this:-
import com.atlassian.jira.component.ComponentAccessorimport com.onresolve.jira.groovy.user.FieldBehavioursimport groovy.transform.BaseScript@BaseScript FieldBehaviours behavioursdef sampleCascade = getFieldById(fieldChanged)def sampleCascadeValue = sampleCascade.value as ListsampleCascade.clearError()def customFieldManager = ComponentAccessor.customFieldManagerdef optionsManager = ComponentAccessor.optionsManagerdef customField = customFieldManager.getCustomFieldObjects(issueContext.projectId, issueContext.issueTypeId).findByName('Sample Cascade')def fieldConfig = customField.getRelevantConfig(issueContext)def options = optionsManager.getOptions(fieldConfig)if (sampleCascadeValue) { def parentOption = options.find { it.value == sampleCascadeValue.first() } def childOption = parentOption?.childOptions if (childOption.size() > 0 && sampleCascadeValue.size() < 2) { sampleCascade.setError('Child List Must Be Selected') }}
Please note that the sample working code above is not 100% exact to your environment. Hence, you must make the required modifications.
The modification is required to ensure that the validation will only trigger when the Parent option selected from the Cascade Select List has Child option(s). If the Parent does not have any child options the validation will not trigger.
Below is a screenshot of the Behaviour configuration:-
I am also including a couple of test screenshots for your reference:-
1. When the Cascading Select List has both Parent and Child options, the Validation error message is displayed as expected as shown in the screenshot below:-
2. If the Cascading Select List only has options from the Parent List and none from the Child List, the error message is not displayed as expected as shown in the screenshot below:-
I hope this helps to solve your question. :-)
It looks like you're new here. Sign in or register to get started.