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

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

7 answers

1 accepted

This widget could not be displayed.

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

 

 

 

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...

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

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

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.

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

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

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

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

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

Using Script Runner : 3.1.4 JIRA : 6.4.9

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>.

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.

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.

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?

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

This widget could not be displayed.

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?

This widget could not be displayed.

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.

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

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

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) 
}

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.

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()...)

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 ?

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

This widget could not be displayed.

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? 

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

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

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.

This widget could not be displayed.

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

This widget could not be displayed.

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.

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

This widget could not be displayed.

I use this Script 

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

and meet this Error. Please heplCacasding Field.png

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Aug 06, 2018 in Jira Service Desk

A is for Activate: Share your top Jira Service Desk onboarding tips for new users!

Hi, everyone! Molly here from the Jira Service Desk Product Marketing Team :).  In the spirit of this month's  august-challenge, we're sourcing stories of Jira Service Desk activation fro...

576 views 25 15
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you