Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

 

3 answers

1 accepted

2 votes
Answer 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

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

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Trello

🏊‍♀️ Atlympic Event: Trello

Hello Community!  Quick disclaimer: We are running a contest on Community (The Atlympics!) from July 23rd - August 8th of 2021. If you are interested in participating in this contest (prizes! ...

108 views 1 11
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you