Behaviour plugin: Show/Hide customfield in Create screen depending on the value of a cascading select list

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

 

2 answers

1 accepted

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

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?

Hi Begona, Could you please attach the logs ?

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

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!

Begona , I updated the script in order to return the child option

It has worked perfectly. Many thanks Thanos!!

One more question (sorry for my ignorance). Which is the difference between optionMap.getAt(0) and optionMap?.getAt(0) ?

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.

Now I understand, many many thanks for your help

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Sunday in Agility

You asked for it, so we delivered: images on issues have arrived

A picture tells a thousand words. And agility boards have just released their latest feature: cover images on issues – so now your board can tell a story at first glance. Upload attachmen...

922 views 3 12
Read article

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