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

Hiding field using behaviour

neeta jain October 9, 2020

Hi Team,

I want hide a text field depending on the value of select list field. written a below script but not getting expected output.

Can you please check and let me know where I am wrong:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager

def textField = getFieldById("customfield_22695")
def selectList = getFieldById("customfield_22694")

def selectListValue = selectList.getValue()

if (selectListValue == "None")
{
textField.setHidden(true)
}
else
{
textField.setHidden(false)
}

2 answers

1 accepted

Suggest an answer

Log in or Sign up to answer
1 vote
Answer accepted
Peter-Dave Sheehan
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.
October 9, 2020

Hi

NONE is not an actual value for the select list. It's only a place holder when no values are selected.

So you can do a simple groovy truthy check like this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager

def textField = getFieldById("customfield_22695")
def selectList = getFieldById("customfield_22694")

def selectListValue = selectList.value

if (selectListValue){
textField.setHidden(true)
} else {
textField.setHidden(false)
}
neeta jain October 19, 2020

Thanks Peter,

It worked, thanks once again.

Regards,

Neeta Jain

neeta jain October 21, 2020

Hi Peter,

If I want to modify my script to hide the field for the options called None and No.

The field should be visible for the value Yes, Can you check my below script is ok or need some modification:

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager

def textField = getFieldById("customfield_22695")
def selectList = getFieldById("customfield_22694")

def selectListValue = selectList.value

if (selectListValue == "None" || selectListValue == "No")
{
textField.setHidden(true)
}
else
{
textField.setHidden(false)
}

 

The above script is working for Yes and No value,, but not for None, kindly suggest teh correction.

 

Regards,

Neeta Jain

Peter-Dave Sheehan
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.
October 21, 2020

One of these should work:

if (!selectListValue || selectListValue == "No")
if (selectListValue == '' || selectListValue == "No")
neeta jain October 22, 2020

Thanks Peter..

Second one worked.

0 votes
Zeeshan Ahmed Khan
Contributor
January 30, 2023

Where do i get the field id's for the custom fields that I have created ?

Peter-Dave Sheehan
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.
January 30, 2023

From the Custom Fields page, you can Edit a field or view the Field's configuration and you'll see the ID as part of the URL in either the edit or configure screen.

Zeeshan Ahmed Khan
Contributor
January 30, 2023

Thanks Peter, I got to know the custom field ID bia the URL . However, the automation doesn't seem to work. My objective is to Unhide "Impacted Service:" text field(custom) as soon as the user selects "Other" in the Affected Service field(custom). "Impacted Service :" field remains hidden on all the screens until then.

The following is my JSON code:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager

def ImpactedServiceField = getFieldById("customfield_10069")
ImpactedServiceField.setHidden(false);

 

What am i missing here ?

Peter-Dave Sheehan
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.
January 30, 2023

This is Groovy code, not JSON.

You can't unhide a field that's hidden by the system.

Make sure your field is available for the current context, it has been added to the screen and is set to visible in the project field config. 

Then and only then will you be able to set to hidden or visible at will using your scriptrunner/groovy code.

Zeeshan Ahmed Khan
Contributor
January 30, 2023

Alright, I have replaced the Groovy code with JSON (see below) and all the other pre-requisites are met like it has been added to the screen/context and is visible. How to make the field hidden now ?

 

Screenshot 2023-01-31 144437.png

Peter-Dave Sheehan
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.
January 30, 2023

Behaviour on Server/DC doesn't support JSON. Only groovy. 

Everything about this question was about Server/DC

If you're on Cloud, you'll be better off asking a brand new question and hopefully, someone else can help you. I have no experience with Scriptrunner on Cloud.

Zeeshan Ahmed Khan
Contributor
January 30, 2023

Alright, thanks !

TAGS
AUG Leaders

Atlassian Community Events