Workaround solution for making 2nd drop down required in multi-cascade custom field?

Eva
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.
October 20, 2011

I know there is a known issue for this, so I was wondering if anyone knows any workaround solution for making the 2nd drop down required. Is there any plugin or a way we can validate and enforce this rule?

Thanks in advance

9 answers

1 accepted

6 votes
Answer accepted
JamieA
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.
October 22, 2011

Update 22 Sep 2015: There are Simple Scripted Validator examples with cascading selects, here: https://scriptrunner.adaptavist.com/4.1.3-SNAPSHOT/jira/recipes/workflow/validators/simple-scripted-validators.html#_validating_cascading_selects

The problem with that code is that it retrieves the issue from the database, and doesn't reflect what's currently in the form. The behaviours plugin is about interacting with the user while the form is being edited, which means you need to get the form value by calling getFormField(getFieldByName(...)).getValue() or similar.

But, I don't think the behaviours plugin supports cascading selects...

Here's another solution if you have the script runner plugin. Add a validator to your workflow of type "Script Validator", then choose the built-in script "Simple Scripted Validator", then enter this for the condition,

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

and put your error message in too.

Update: As of some jira version, (5.2 ?) the field value seems to be stored as a map, so to check both parts of a cascading select are filled use:

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

 

 

 

Eva
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.
October 23, 2011

Yea, I tried so many ways with behaviours and it doesnt seems to work on cascading select (perhaps an improvement point?)

I am not too sure about the script validator as I want the error to appear whne the user submit the form and not when it proceed to the next step...

Eva
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.
October 23, 2011

I have mistaken your suggestion before. I didnt know you can add validation on create within workflow (never have to do it so far). Your suggestion works!!!! Thank you! I reallly wish JRA-10302 is fixed b/c this is definitely a bug :T

Thanks again

Eva
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.
October 23, 2011

I just realize it only works for create but if you edit, it doesnt check that step. Since edit is not really part of the workflow (or is it?), how can you add similar validation for the cascade select?

Thanks again

JamieA
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.
October 23, 2011

Yeah, that's problematic. Not sure if there is an outstanding request for this in the behaviours plugin.

Note also somehow I missed two crucial question marks in my line above - without these you will get a NullPointerException if the first part of the cascading select is empty.

Bryan Karsh
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.
May 22, 2013

Hi Jaimie,

Having problems getting the following cascade selection field (Service Affected) validation to work in a workflow:

cfValues["Service Affected"]?.getKeysAndValues()?.size() == 2
I get the following:
 ERROR bryank 596x729001x1 osaq3c 66.54.212.165,10.110.199.61 /secure/QuickCreateIssue.jspa [groovy.canned.utils.ConditionUtils] javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: java.util.HashMap.getKeysAndValues() is applicable for argument types: () values: []


I am running Jira 5.26, Script Runner 2.1.3

JamieA
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.
May 23, 2013

Hi bryan, jira api seems to have changed... I updated my answer.

Bryan Karsh
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.
May 23, 2013

Jamie -- you rock! that did the trick. Thank you very much!

Marco Colageo June 17, 2013

Hi All,

I want to validate the parent option of a cascading select list rejected the None option. I set the validator as follows:

cfValues['Cascading Field'].getValue() != ""

That does not work. This field is required via the Field Configuration. What's wrong with the validator?

Many thanks

Marco

JIRA v5.2.4

Avdhesh Chauhan September 23, 2015

Hi Jamie, I tried your both solutions but didnt work for me: got errors like this: Hi , I also tried both the script given but got error in logs onUtils] Condition failed on issue: ARQ-8720, built-in script:com.onresolve.scriptrunner.canned.jira.workflow.validators.SimpleScriptedValidator groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.keySet() is applicable for argument types: () values: [] Possible solutions: toSet(), toSet(), set(int, java.lang.Object), set(int, java.lang.Object), get(int), get(int) at Script16.run(Script16.groovy:1) 2015-09-23 14:11:38,397 ajp-bio-8009-exec-33 ERROR avdhesh.chauhan 851x39158x1 8btozy 10.129.15.65 /secure/CommentAssignIssue.jspa [canned.jira.utils.ConditionUtils] Script follows: cfValues.get("Query Type")?.keySet()?.size() == 2 Please help. Thanks Avdhesh

Avdhesh Chauhan September 23, 2015

Using Script Runner : 3.1.4 JIRA : 6.4.9

Jamie Echlin _ScriptRunner - The Adaptavist Group_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
September 24, 2015

It can't be a cascading select then. Are you sure it's a standard cascading select and not one provided by a plugin that has more than two levels? A cascading select returns a Map<String, Option>.

JamieA
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.
September 24, 2015
Indranil Mondal December 28, 2015

Hi Jaimie, should this work for multi select cascading list ( 4 lists) too? Its working fine for cascading list with 2 lists but not with 4, any reason? I have just replaced the 2 with 4 in this case. BTW the multi select cascading list is a trial version of the add on installed in my JIRA.

JamieA
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.
December 29, 2015

Multi select cascading list is not supported by the behaviours plugin, but you should be able to use it fine with simple scripted validator or any script validator function. I'm not really sure what you're saying is not working, probably best to open a new question.

Nicolas Bourdages
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 2, 2016

I tried this code in a scripted validator:

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

It works well when creating an issue the normal way, but Service Desk throws an error "This issue can't be created through Service Desk" every time and won't let me create tickets regardless of the values in the cascading field.

Is there a workaround?

Like Sam likes this
Jamie Echlin _ScriptRunner - The Adaptavist Group_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
March 2, 2016

What version of JSD? It used to be completely broken with respect to workflow validators, eg https://jira.atlassian.com/browse/JSD-221.

2 votes
NguyenThanh Hoa May 16, 2018

I use this Script 

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

and meet this Error. Please heplCacasding Field.png

David Lawless January 3, 2019

Hi 

Using Jira 7.10 I have also been getting Static Type errors, but when using

"cfValues.get['your_ cascading_field_name']?.key.set()?.size() == 2" as a simple scripted validator, as per instructions at:

https://scriptrunner.adaptavist.com/5.4.43/jira/recipes/workflow/validators/simple-scripted-validators.html

I found this returned the 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.

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 you or someone else facing the same problem.

srmission July 9, 2019

Hi,

 

I could achieve this using Validators for Create option in workflow. I used groovy script. This is what I have used:-

issue.get("customfield_12504")?.get("1")?.optionId

0 votes
Oded Mozesh December 16, 2019

import java.util.Collection;
(cfValues["Discovery Stage"] as HashMap)?.keySet()?.size() == 2

 

Works, but if a first value has no second value related to it - you won't be able to select it

0 votes
Sandra Meessen March 19, 2019

Hi all, can someone help me with this one, similar to the original question. I have a cascading field, parent part and child part. I want the child part to be mandatory if you select a valid value in the parent part. This must be checked in the edit issue mode. How do I do that (I'm not experienced with scriptrunner coding.)? I use Jira Software v7.11. Thanks for the help. 

David Lawless March 19, 2019

Hi - see my post above from Jan 03 2019.

The only way I have been able to make the child part mandatory is on workflow transitions and not the edit screen.  This wasn't too bad for us as we placed on the create transition meaning both had to be completed when creating an issue.

You do this by adding a simple scripted validator on the workflow transition(s).  The code is:

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

def issue = issue as MutableIssue
def cascadingSelect = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue)?.findByName("Name_of_your_CascadingField")
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

It does mean it can still be edited but in reality would someone edit to remove the child part?  Depending on your exact requirements you could experiment with having the validator on multiple workflow transitions so if someone did remove it would be required again when the issue was moved.  You could always remove the cascading field from the edit screen as well. 

burton_roger2 March 11, 2020

Hi David,

I am also trying to do the same as Sandra but during issue Create.

" I have a cascading field, parent part and child part. I want the child part to be mandatory if you select a valid value in the parent part."

I've tried your simple script code (above) but it does not work.

Your assistance is greatly appreciated!

Jira v8.5.1

0 votes
monika singh May 11, 2017

To make second field of cascading custom field mandatory, you need to perform changes in the workflow.

go to the workflow associated with  that cascading field, put a field required validator  for cascading field in that work flow, and it will start working.

Eva Fernandez July 11, 2017

I have the same problem. My JIRA is in CLOUD, and I have runner script, but I can not include script as validation .. any other ideas?

image.png

Carol Jones February 26, 2019

@monika singh -- as of Jira v7.13 and JSU v2.11, making the field required through a workflow validation only requires that a user populates the parent option of the cascading field instead of both the parent and child

0 votes
valeria cruz May 5, 2017

I traied all values, but dosent work. Some news about this tip??

0 votes
Alastair Batchelor July 2, 2015

This is a great little tip but I am having trouble getting it to work as expected. When trying to save the issue the error triggers even when both parts of the field are populated. Any ideas why this may be occurring? 

JamieA
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.
July 5, 2015

I just tested this and it worked fine... can you attach a screenshot of the workflow function configuration.

Suresh July 23, 2015

Hi Jamie, I have added validation on create transition for validation of casecade field as required for both drop downs.But is not working.It showing error message that we have added while adding post function.I am missing anything. cfValues.get("CascadingSelect")?.keySet()?.size() == 2

Avdhesh Chauhan September 23, 2015

Hi , I also tried both the script given but got error in logs onUtils] Condition failed on issue: ARQ-8720, built-in script:com.onresolve.scriptrunner.canned.jira.workflow.validators.SimpleScriptedValidator groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.keySet() is applicable for argument types: () values: [] Possible solutions: toSet(), toSet(), set(int, java.lang.Object), set(int, java.lang.Object), get(int), get(int) at Script16.run(Script16.groovy:1) 2015-09-23 14:11:38,397 ajp-bio-8009-exec-33 ERROR avdhesh.chauhan 851x39158x1 8btozy 10.129.15.65 /secure/CommentAssignIssue.jspa [canned.jira.utils.ConditionUtils] Script follows: cfValues.get("Query Type")?.keySet()?.size() == 2 Please suggest.

0 votes
JamieA
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.
October 20, 2011

Responding to your comment, there is an example of checking the values of cascading select lists here: https://studio.plugins.atlassian.com/browse/GRV-42

So if cfVal.getAllValues()[1] is null you can throw an InvalidInputException.

Eva
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.
October 21, 2011

Jamie, thanks for your quick reply, that example is what i was looking for.

I put this code into the behaviour plugin but for some reason even when I pick value for second drop down, it somehow cache the old value and thus error out in an infinite loop (or so it seem)

I thought the error would reevaluate when user press "Update/Create"? Thoughts?

Thanks again for your help :)

JamieA
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.
October 21, 2011

That code is for the script runner plugin... is that what you meant?

Eva
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.
October 21, 2011

I actually put the code in behaviours plugin -> server side scripts. It compile fine but it doesnt work like i wanted. When I create the issue, it bypass the validation, and when I edit with both values filling in, it just keep erroring out. Here is the code (I think that might be the easiest)

import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.customfields.view.CustomFieldParams
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.ComponentManager
import java.util.Collection;

ComponentManager componentManager = ComponentManager.getInstance()
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
MutableIssue issue = componentManager.getIssueManager().getIssueObject(formContents["id"] as Long)
CustomField cf = customFieldManager.getCustomFieldObjectByName("Categories")
CustomFieldParams cfVal = issue.getCustomFieldValue(cf) as CustomFieldParams

FormField formfield = getFieldByName("Categories")
String warning = "Please select both categories"

if (cfVal){
         Collection values = cfVal.getAllValues()
         String v1 = values[0]
         String v2 = values[1]

         if (v2 == null) {
            formfield.with {
                setValid(false)
                setTitle (warning)
                setHelpText("&lt;div class=\"warningBox\"&gt;$warning&lt;/div&gt;")
            }
        }
        else
            formfield.setValid(true) 
}

Nick March 16, 2014

Jamie,

I have been trying your revised solution, i.e. :

1
cfValues.get("CascadingSelect")?.keySet()?.size() == 2

but inside a script, as I will need to run the test from a number of workflow transitions.

I have therefore used :

import com.atlassian.jira.issue.Issue
import com.opensymphony.workflow.InvalidInputException

if (cfValues.get('Release - Stream')?.keySet()?.size() == 2) {

//force both fields to be filled in

invalidInputException = new InvalidInputException("You must fill in both fields")

}

and get this error :

MissingPropertyException: No such property: cfValues for class

any assistance greatly appreciated.

JamieA
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 17, 2014

I'm not sure what plugin you are talking about... either way, if cfValues is not available, you need to get the cf value yourself, eg:

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def customField = customFieldManager.getCustomFieldObjectByName("Release - Stream")
def cfVal = issue.getCustomFieldValue(customField) as Map

if (cfVal.keySet()...)

Nick March 17, 2014

Thanks Jamie,

the example you gave didn't quite work, as 'ComponentAccessor' wasn't recognised. Presumably because I was only doing :

import com.atlassian.jira.issue.Issue
import com.opensymphony.workflow.InvalidInputException

adding

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField

seems to have done the trick.

Where can we find the full details and guidelines on these 'things' (i.e. the imported elements) that we need in order to get at JIRA data ?

Robert Mota December 11, 2014

Hi Nick, To use ComponentAccessor you must import {code} import com.atlassian.jira.component.ComponentAccessor {code} Do not use ComponentManager in last versions of Jira API. To find all java doc of Atlassian API : google search, according to componentaccessor it was "jira api componentaccessor" You will find https://docs.atlassian.com/jira/latest/com/atlassian/jira/component/ComponentAccessor.html Best regards

0 votes
Eva
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.
October 20, 2011

I am also toying with the post function or groovy script idea - but not sure how to check the value fo the second drop down list. Thoughts?

Suggest an answer

Log in or Sign up to answer