Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Hidde Fields based on checkbox selection using ScriptRunner Behaviours

Naqibullah Azizi December 11, 2023

Hello Friends,

i want to hide/show some custom fields on a form using ScriptRunner Behaviours for a custom field checkbox: "Validation Type".

Unfortunately, it doesn't work as aspekted:

- it shows all custom fiels initial
- it doesn't hide (dynamically) the wished field when uncheck a box 

 

Here is my code:

 

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import groovy.transform.BaseScript

def FormField checkboxField = getFieldByName('Validation Type')
List<String> selectedOptions = checkboxField.getValue() as List<String>

// My fields to hide/show
def FormField field1 = getFieldByName('Assignee/Team: ART')
def FormField field2 = getFieldByName('Reporter: one-time validation')
def FormField field3 = getFieldByName('Customer/AE')

if (selectedOptions.contains('Assignee/Team: ART')) {
    field1.setHidden(false)
} else {
    field1.setHidden(true)
}
if (selectedOptions.contains('Reporter: one-time validation')) {
    field1.setHidden(false)
} else {
    field1.setHidden(true)
}
if (selectedOptions.contains('Customer/AE')) {
    field1.setHidden(false)
} else {
    field1.setHidden(true)
}

 

1 answer

1 accepted

0 votes
Answer accepted
Florian Bonniec
Community Champion
December 11, 2023

HI @Naqibullah Azizi 

 

Where did you set this script ? As initializer or in the server side script on the custom field in the Behaviour config ?

 

It has to be on the field not the initilizer otherwise it will not run when the field value is updated.


Regards

Naqibullah Azizi December 12, 2023

Hi @Florian Bonniec ,

it's the script on the field "Validation Type".

 

Best Regards
Naqibullah

Florian Bonniec
Community Champion
December 12, 2023

Hi @Naqibullah Azizi 

One of the reason could be the the selectedOptions line, something weird with the checkbox is that if you select only 1 option, the result is a String not an array.

Could you try this instead to get the selectValues ?

List<String> selectedOptions = checkboxField.getValue() instanceof String ? [checkboxField.getValue()] as List<String>: checkboxField.getValue() as List<String>

Regadrs

Naqibullah Azizi December 18, 2023

I'll try it out, thank you a lot :)

Naqibullah Azizi December 27, 2023

Hi Florian,

thank you very much it worked :)

 

But i have now the following problem:

The Text Fields stay visible when unchecking the represented checkbox.

 

Here my (new) Code:

 

import com.onresolve.jira.groovy.user.FormField

def FormField checkboxField = getFieldByName('Naqibullah Test Checkboxes')
List<String> selectedOptions = checkboxField.getValue() instanceof String ? [checkboxField.getValue()] as List<String>: checkboxField.getValue() as List<String>

// My fields to hide/show
def FormField field1 = getFieldByName('Naqibullah Test Textfield 1')
def FormField field2 = getFieldByName('Naqibullah Test Textfield 2')

if (selectedOptions.contains('Test 1')) {
    field1.setHidden(false)
} else {
    field1.setHidden(true)
}
if (selectedOptions.contains('Test 3')) {
    field2.setHidden(false)
} else {
    field2.setHidden(true)
}

Suggest an answer

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

Atlassian Community Events