Show/Hide field Based Cascading field selection

John River June 6, 2018

I have a cascading field called human.

When a user selects country on the first part of the cascading field. and selects Doctor on the second part of the cascading field. The field Total need to appear. For any other option, the total field must be hidden.  I am using scriptrunner behaviour for this one.

 

4 answers

1 accepted

2 votes
Answer accepted
Alexey Matveev
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.
June 6, 2018

Hello,

You should use ScriptRunner behaviours

1. Create a behaviour and add the cascading custom field to the behaviour.

2. Write a script for the added cascading field. Your script should be like this:

def cascField = getFieldByName("cascading field name")

def hiddenField = getFieldByName("name of your hidden field")

if (cascField.getValue().get("1") == "your value") {

      hiddenField.setHidden(true)

}

else {

   hiddenField.setHidden(false)

}
Alexey Matveev
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.
June 7, 2018

I just checked the code. It turned out that cascading fields work differently in behaviours. The value is actually an ArrayList. That is why your script should look like this:

def cascField = getFieldByName("cascading field name")

def hiddenField = getFieldByName("name of your hidden field")

def value = cascField.getValue() as ArrayList<String>

if (value.size() == 2 && value.get("1") == "your value") {

      hiddenField.setHidden(true)

}

else {

   hiddenField.setHidden(false)

}
Like RDaridan likes this
John River June 11, 2018

I tried it and I get an error on the value.get("1") and I think this is because get only takes an integer.

0 votes
devsuresh2005 May 4, 2021

Hi All,

Following code has worked for me but ensure the cascading custom field is available in the screen you are working on,

For Behaviour:

def cfield = getFieldByName("cascading custom field name")
Map mapValue = cfield.getValue() as Map
def value1 = mapValue.get(0).toString()
def value2 = mapValue.get(0).toString()

For Post Functions:

import com.atlassian.jira.component.ComponentAccessor

def issueManager = ComponentAccessor.getIssueManager()

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def customField = customFieldManager.getCustomFieldObjectsByName('cascading custom field name').getAt(0)
def cstFldVlue = issue.getCustomFieldValue(customField)

Thanks

Silu patra September 26, 2023

Hi Suresh,

Can you please share the complete behaviour code that you have implemented to pull in the desired result.

0 votes
Ramya Mani January 10, 2019

Hello can anyone help me getting the cascading value in a custom field in behaviors.

My custom field has values in the following way:

Category[A,B,C]

If A is selected then the cascading list will show more values [A1,A2,A3].

I declared a Map variable and i tried many approaches. But I am going wrong somewhere. Would really appreciate some help here.

Eshwar Palem April 12, 2020

@Ramya Mani,

 

Did you resolve this? If yes, I would like to know the approach. The script is working fine, but the behaivours functionality seems to be incorrect in case of cascading,as it is not waiting for the child option to get selected and taking the first default child value.

Is there anything which I am doing? Please help me out here.

0 votes
John River June 7, 2018

 Hi @Alexey Matveev

 

I get an error on the if condition

 

The error is: Please check if the declared type is right and if the method exists

David STOCKY June 7, 2018

def cascField = getFieldByName("cascading field name")
def hiddenField = getFieldByName("name of your hidden field")

//hide by default
hiddenField.setHidden(false)

//create map
Map cfVal = cascade.getValue() as Map
if (cfVal){
if (cfVal.get(0) == "your value") {
hiddenField.setHidden(true)
}
}

David STOCKY June 7, 2018

you need to declare a map variable

John River June 7, 2018

hi @David STOCKY

 

This is my script

 

Other = getFieldById("customfield_10177")

 

if(status == "Apollo")    

{        apolloCategory.setHidden(false)

        apolloCategory.setRequired(true)

        apolloCategory.setHelpText("The Apollo category field is required, please provide information")   

            def cascField = getFieldByName("Apollo Category")
//create map

Map cfVal = cascField.getValue() as Map

if(cfVal) 

  {

        if(cfVal.get(0) == "Other")        

          Other.setHidden(true)        }

    }                        

  }

 

There are no errors now, but the other field is just not coming up or hidding

David STOCKY June 7, 2018

Is your field on the screen?

On which screen do you want to make it hide depending on cascading value? Create? Edit? View? Service Desk Portail?

David STOCKY June 7, 2018

To debug, you can add this code.

It will display information in jira log files

 

import org.apache.log4j.Logger
import org.apache.log4j.Level

def log = Logger.getLogger("com.behaviours")
log.setLevel(Level.DEBUG)

log.debug("text to display on log")

John River June 7, 2018

It's on the Create screen.

David STOCKY June 7, 2018

Are you sure that your customfiel (customfield_10177) is on the screen "Create" for your project?

Joe Pitt
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 7, 2018

@David STOCKY When does the script run? If when the screen appears the select fields will be empty. Wouldn't it need to run after the cascading field is populated? Could that be the problem? 

David STOCKY June 7, 2018

Normaly, the behaviours scripts should be on cascading field update if the script is define as server side.

In addition, behaviours mapping should be done on the right Project / issue type

Suggest an answer

Log in or Sign up to answer