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

Begoña Bonet November 26, 2015

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

 

3 answers

1 accepted

2 votes
Answer accepted
Thanos Batagiannis _Adaptavist_
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.
November 27, 2015

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

0 votes
Sema YILDIRIM July 1, 2021

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

0 votes
Begoña Bonet November 27, 2015

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?

Thanos Batagiannis _Adaptavist_
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.
November 30, 2015

Hi Begona, Could you please attach the logs ?

Thanos Batagiannis _Adaptavist_
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.
November 30, 2015

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

Begoña Bonet November 30, 2015

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!

Thanos Batagiannis _Adaptavist_
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.
November 30, 2015

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

Begoña Bonet November 30, 2015

It has worked perfectly. Many thanks Thanos!!

Begoña Bonet November 30, 2015

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

Thanos Batagiannis _Adaptavist_
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 1, 2015

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.

Begoña Bonet December 1, 2015

Now I understand, many many thanks for your help

Suggest an answer

Log in or Sign up to answer