How do I make the second field of a cascading select field required?

JT
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 4, 2013

If a cascading select field is set to required, only the first selection field is actually required. The second field can be left unselected. How do I make both fields required? I found this:

cfValues["myCascadingSelect"]?.getKeysAndValues()?.size() == 2

but have been unsuccessful in getting it to work as a validation on the create transition.
We are running JIRA v5.2.5

7 answers

1 accepted

1 vote
Answer accepted
darylchuah
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 5, 2013

Hi there

There is a similar question regarding on this posted before, perhaps you can refer on this as a general guideline and reference : https://answers.atlassian.com/questions/17148/workaround-solution-for-making-2nd-drop-down-required-in-multi-cascade-custom-field

Hope it helps :)

Cheers

2 votes
Ma Hao August 20, 2014
cfValues['your cascading field']?.values()*.value.size() == 2
 
This one is working for me.
gonzalo zegarra June 16, 2015

Hi Where do I have to write that code?

Ma Hao June 16, 2015

You need to install script runner plugin firstly, Then use Script Validator to add the code

Amir Katz (Outseer)
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.
March 27, 2018

I tried it in a workflow transition validator (using the option 'Simple scripted validator') and it does not seem to work.

I read in another post that since Jira 5.x, the values are returned as a map, so they suggested this check:

   cfValues.get("customfield_17637")?.keySet()?.size() == 2

but it does not work for me (meaning when I put a not-None value, it still fails).

Anyone?

NguyenThanh Hoa May 16, 2018

I use this Script 

"cfValues['Department and Service']?.values()*.value.size() == 2"

and meet this Error. Please hepl

Cacasding Field.png

Sakshi Mittal October 11, 2018

Hi ,

I am trying to implement this , however I get the same error.

My requirement is a bit different however. I want that second field of cascading select field is mandatory only if the first field is not empty.

 

Is anyone able to implement this successfully?

 

Best Regards

Sakshi

David Lawless January 3, 2019

I'm using cfValues["Expected Time"]?.keySet()?.size() == 2 as per https://scriptrunner.adaptavist.com/4.3.4/jira/recipes/workflow/validators/simple-scripted-validators.html#_validating_cascading_selects

However this will not work, returns following error - [Static Type Checking] - Cannot find matching java.lang.Object#keyset(). Please check if the declared type is correct and if the method exists. Possible solutions: getAt(java.lang.String) @ line 1, column 1

Am on Jira v7.10

David Lawless January 3, 2019

Further to above:

I raised a ticket with Adaptavist and was told "Static type checking errors can sometimes be ignored, and this is one of those cases. The code should still work fine, even though an error is shown. That being said, you can try using this code instead and seeing if that gets rid of the error:

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

def issue = issue as MutableIssue
def cascadingSelect = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue)?.findByName("CascadingSelect")
def values = issue.getCustomFieldValue(cascadingSelect) as HashMap

// Will return true if both the parent and child options in the Cascading Select have a value
values?.keySet()?.size() == 2

I haven't tried ignoring the static type error, but have tried the code provided and it worked a treat.  Hopefully may help someone

Like # people like this
Lakshmi S July 12, 2023

@Ma Haoao's solution worked for me.

Use "Simple Scripted Validator" on the workflow validator, and ignore errors.

code.PNG

 

 

Output is :

 

code1.PNG

Like NguyenThanh Hoa likes this
1 vote
Michael Spoonauer October 25, 2020

If you have the requirement of only requiring a child option if the parent happens to have any children, I found that the following logic worked for me as a simple scripted validator using the ScriptRunner plugin:

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.customfields.option.Options

CommentManager commentManager = ComponentAccessor.commentManager
OptionsManager optionsManager = ComponentAccessor.optionsManager

def cascSelectField = customFieldManager.getCustomFieldObjects(issue)?.findByName("Your Cascading Field Name")

def cascSelectFieldValues = issue.getCustomFieldValue(cascSelectField) as Map

def parentAndChildOptionsHaveValues = cascSelectFieldValues?.keySet()?.size() == 2

def aParentOptionHasBeenSelected = false

def thereAreNoChildOptionsToSelect = false

if (!parentAndChildOptionsHaveValues) {
def cascSelectParentOption = cascSelectFieldValues[null] as Option
def cascSelectParentOptionValue = cascSelectParentOption?.value

if (cascSelectParentOptionValue) {
aParentOptionHasBeenSelected = true

def cascSelectFieldAllOptions = optionsManager.getOptions(cascSelectField.getRelevantConfig(issue)) as Options

def parentOption = cascSelectFieldAllOptions.find {
it.value == cascSelectParentOptionValue
} as Option

def childOptions = parentOption?.childOptions

if (!childOptions || childOptions?.size() == 0) {
thereAreNoChildOptionsToSelect = true
}
}
}

/*
* Return a truthy value only if either both a parent and child option have been selected
* or a parent option has been selected but there are no child options to select
*/
parentAndChildOptionsHaveValues || (aParentOptionHasBeenSelected && thereAreNoChildOptionsToSelect)
Brian Keefe November 19, 2021

^^ this is the business. Thank you @Michael Spoonauer. I did have to add this on the validator script:

import com.atlassian.jira.component.ComponentAccessor

Jira DC 8.17.1 and SR 6.36.0

Like NguyenThanh Hoa likes this
0 votes
Lender November 23, 2020

@JT4 ,

You can try this with behaviours:

Script: 

//23/11/2020 [Leandro]
//Script que deja el segundo campo de select cascading obligatorio

def firstSelect = getFieldById(getFieldChanged())
def secondSelect = getFieldById(getFieldChanged()+":1");

//Si la sub-opcion no estuviese seleccionada, emite error
if (firstSelect.getValue() != [] && secondSelect.getValue() == null){
firstSelect.setError("Informe la opcion principal y la sub-seleccion.");
} else{
firstSelect.clearError();
}

 

Second field of a cascading select field required - Behaviours.JPG

 

0 votes
Gyorgy Dani-Rauf September 15, 2020

You can try this:

def CustomField = getFieldById(getFieldChanged())
def CustomFieldValue = CustomField.getValue()
def parentOption = CustomFieldValue.getAt(0)?.value
def childOption = CustomFieldValue.getAt(1)?.value

//if both has a value then both is selected
if( (parentOption && childOption) {
...
}

We use this in behaviours.

0 votes
burton_roger2 March 11, 2020

This is still an issue. Need the second field of a cascading selection to also be required. can someone please provide the correct solution since this post has indicated "Answer accepted".

I've tried all scripts mentioned above with no luck. Currently using the following:

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

def issue = issue as MutableIssue
def cascadingSelect = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue)?.findByName("XXXX")
def values = issue.getCustomFieldValue(cascadingSelect) as HashMap

// Will return true if both the parent and child options in the Cascading Select have a value
values?.keySet()?.size() == 2

 

I need both fields to be required. Is this possible?

 

Thank you in advance for any assistance! :)

0 votes
MK January 8, 2020

Try mentioning the custom field with double quotes

Suggest an answer

Log in or Sign up to answer