Scriptrunner behavior to hide show field based on value of checkbox field

Pete P
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 17, 2023

I need assistance with a Scriptrunner behavior to hide/show a field based on value of checkbox field. 

"Origin of Defect" (checkbox field)
"Origin of Defect: Other" (single line of text field)

My checkbox field "Origin of Defect" contains numerous values including "Other".  I want to show and require the "Origin of Defect: Other" field if the "Origin of Defect" field CONTAINS the selection "Other" (ID 12828 in my case).  This works perfectly if ONLY "Other" is selected, however if multiple checkboxes are selected it does not work.  

How can I update to accept multiple selections and still get desired behavior ?


def od= getFieldByName("Origin of Defect")
def odO = getFieldByName("Origin of Defect: Other")

odO.setHidden(true)

def odValue = od.getFormValue();

if (odValue =="12828") // Other
{
odO.setHidden(false)
odO.setRequired(true)
}
else
{
odO.setHidden(true)
odO.setRequired(false)

}

1 answer

1 accepted

1 vote
Answer accepted
Florian Bonniec
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 17, 2023

Hi @Pete P 

DOes this script works ?

def od= getFieldByName("Origin of Defect")
def odO = getFieldByName("Origin of Defect: Other")

odO.setHidden(true)

def odValue = od.getFormValue() as List;

if (odValue.contains("12828")) // Other
{
odO.setHidden(false)
odO.setRequired(true)
}
else
{
odO.setHidden(true)
odO.setRequired(false)

}

 

Regards

Pete P
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 17, 2023

Let me check one moment!

Florian Bonniec
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 17, 2023

Yes but the one I posted ?

Pete P
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 17, 2023

@Florian Bonniec  your code DOES work if MULTIPLE checkboxes are selected, as long as one is "Other" !


But... if only one checkbox ("12828")) // Other is selected, it no longer works - very strange.  Thanks for your prompt reply!

Pete P
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 17, 2023

1.PNG2.PNG

Florian Bonniec
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 17, 2023

And this one ?

def od= getFieldByName("Origin of Defect")
def odO = getFieldByName("Origin of Defect: Other")

odO.setHidden(true)

def odValue = od.getFormValue();

if(odValue instanceof String){

odValue = [odValue]

}

if (odValue.contains("12828")) // Other
{
odO.setHidden(false)
odO.setRequired(true)
}
else
{
odO.setHidden(true)
odO.setRequired(false)

}

Florian Bonniec
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 17, 2023

The issue is that the value returned is a String when there is one value selected but an array if there is multiple values selected.

The script above should transform the String value to an array with one value.

 

Regards

Pete P
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 17, 2023

That works PERFECTLY @Florian Bonniec . Thank you for the explanation as well. 

It is throwing up a warning but it works.

 

 3.PNG

Florian Bonniec
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 17, 2023

Yes it's because you did not define odValue so it's consider as an object. We cannot define the type for this as it can be String or Array you can get around by creatin a new variable.

 

def od= getFieldByName("Origin of Defect")
def odO = getFieldByName("Origin of Defect: Other")

odO.setHidden(true)

def odValue = od.getFormValue();

String[] odValues

if(odValue instanceof String){

odValues = [odValue]

}else{

odValues = odValue

}

if (odValues.contains("12828")) // Other
{
odO.setHidden(false)
odO.setRequired(true)
}
else
{
odO.setHidden(true)
odO.setRequired(false)

}

Pete P
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 17, 2023

Thanks @Florian Bonniec , getting another message:

4.PNG

Like Robert likes this
Florian Bonniec
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 17, 2023

it's a simple warning no ?

 

You can add "as List" at the end of the line 16 

Like Robert likes this
Pete P
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 17, 2023

Perfection :)

Many thanks for all your help!!

AndreH
Contributor
September 30, 2024

Hi,

Is there an option to get this to work for the Portal Submission?

To have a checkbox field and if something is selected on the checkbox on the Portal, then another text box field will show

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.2.2
TAGS
AUG Leaders

Atlassian Community Events