Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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 Feb 05, 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

cfValues['your cascading field']?.values()*.value.size() == 2
 
This one is working for me.

Hi Where do I have to write that code?

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

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?

I use this Script 

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

and meet this Error. Please hepl

Cacasding Field.png

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

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

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

@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

 

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)

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.

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! :)

Try mentioning the custom field with double quotes

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you