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
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
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?
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("<div class=\"warningBox\">$warning</div>") } } else formfield.setValid(true) }
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?
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.
I traied all values, but dosent work. Some news about this tip??
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?
I use this Script
"cfValues['Department and Service']?.values()*.value.size() == 2"
and meet this Error. Please hepl
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.ComponentAccessorimport com.atlassian.jira.issue.MutableIssue
def issue = issue as MutableIssuedef 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 valuevalues?.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.
@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
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.
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:
def issue = issue as MutableIssuedef cascadingSelect = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue)?.findByName("Name_of_your_CascadingField")def values = issue.getCustomFieldValue(cascadingSelect) as HashMap
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.
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
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
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
It looks like you're new here. Sign in or register to get started.