In creation screen I need to show or hide a customfield depending on the value selected by user in a cascading select list.
I've included this script but it doesn't works:
if(getActionName() == "Create" && getFieldByName("Área/Subárea").getValue()=="Proyectos,Proyectos menores"){
getFieldByName("Es menor").setHidden(true);
}
if(getActionName() == "Create" && getFieldByName("Área/Subárea").getValue()!="Proyectos,Proyectos menores"){
getFieldByName("Es menor").setHidden(false);
}
What am I doing wrong?
Thanks in advance
Hi Begona,
The cascading list value is an ArrayList with Options, therefore the .getValue() returns to you an ArrayList with the Option ids (as Strings). Below is an example of how to get the id and then through the optionManager to get the value for this id.
import com.atlassian.jira.component.ComponentAccessor def optionManager = ComponentAccessor.getOptionsManager() def optionMap = getFieldByName("Área/Subárea").getValue() //this will give you the option id for the your first (parent) choice def parentOptionId = optionMap?.getAt(0) if(!parentOptionId) return //this will give you the option id for the child choice def childOptionId = optionMap?.getAt(1) if(!childOptionId) return // cast String to Long def longValue = Long.valueOf(childOptionId).longValue() // get the Option with the particular id def option = optionManager.findByOptionId(longValue) def optionValue = option.getValue() //Now you have a String value for the parent Option that you can use in your equality checks if(getActionName() == "Create" && optionValue=="Proyectos"){ getFieldByName("Es menor").setHidden(true); }
Please let me know if you need further assistance
Kind regards
Thanos
can you please help me too? My problem is I have this "Change Type" field. When "deployment" is selected as a child option in this field, I want to bring automatic text to the description field.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I've included this code in the script associated to "Es menor" customfield:
import com.atlassian.jira.component.ComponentAccessor
def optionManager = ComponentAccessor.getOptionsManager()
def optionMap = getFieldByName("Área/Subárea").getValue()
//this will give you the option id for the your first (parent) choice
def parentOptionId = optionMap.getAt(0)
// cast String to Long
def longValue = Long.valueOf(parentOptionId).longValue()
// get the Option with the particular id
def option = optionManager.findByOptionId(longValue)
def optionValue = option.getValue()
//Now you have a String value for the parent Option that you can use in your equality checks
log.debug("Parent option value : ${optionValue}")
if(getActionName() == "Create" && optionValue=="Proyectos"){
getFieldByName("Es menor").setHidden(true);
}
if(getActionName() == "Create" && optionValue!="Proyectos"){
getFieldByName("Es menor").setHidden(false);
}
image2015-11-27 14:12:42.png
But it doesn't work:
image2015-11-27 14:11:51.png
Any idea in what am I doing wrong?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Begona, Could you please attach the logs ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I think I found the problem. You have to assign the script to the 'Area/Subarea' instead of the 'Es Menor'. You want to check the value of the cascading list and whenever this value is changing then hide or shown the Es Menor custom field. I also updated the above script to check for null values (if an option in the cascading list is not available) Please let me know if that does the trick. Regards Thanos
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanos It has worked fine for parent option! But I need the child option value. How can I get this value? Many thanks in advance!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Begona , I updated the script in order to return the child option
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.
One more question (sorry for my ignorance). Which is the difference between optionMap.getAt(0) and optionMap?.getAt(0) ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
It is called safe dereference operator. So in the above code if you use optionMap.getAt(0) and the oprionMap is null (in your case if getFieldByName("Área/Subárea").getValue() is null) then the above code will throw a java.lang.NullPointerException: Cannot invoke method getAt() on null object at Script1.run(Script1.groovy:9) The ? operator will cause the above call to fail in 'silent' (without null exceptions). I think is a big discussion and it depends on whether you want the exception to be thrown (and handled accordingly) or not. To be honest I am trying to adapt such kind of groovy's features in order to make the code more "laconic", cause I just want the code to be executed for valid references and just be silent otherwise. Hope my answer makes sense.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.