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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,458,239
Community Members
 
Community Events
176
Community Groups

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

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

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
TAGS

Atlassian Community Events