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
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I use this Script
"cfValues['Department and Service']?.values()*.value.size() == 2"
and meet this Error. Please hepl
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Ma Haoao's solution worked for me.
Use "Simple Scripted Validator" on the workflow validator, and ignore errors.
Output is :
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
^^ 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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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();
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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! :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.