Show/ Hide multiple custom fields based on multiple Checkbox options in JIRA Data Center
Asked by Digvijay Singh Gehlot:

Hi Community,

I have created a Custom Checkbox field with multiple options and three custom fields: Text(Single-Line).

The Checkbox field contains three options:

* Alpha, Beta, Gamma

Based on the selection of options, the respective Text field should show/ hide.

For example:

* Checkbox: Alpha and Gamma are selected as options
* Then, Text fields such as: Alpha ID and Gamma ID should show and Beta ID should hide.

So, based on selection of Checkbox options, I want to show/ hide the Text fields.

Please guide on how I can achieve this using Scriptrunner, JMWE app, JSU, etc?

Thanks


Answers:
author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Digvijay Singh Gehlot](/t5/user/viewprofilepage/user-id/5431110)

I have provided a very similar solution in this [Community Post](https://community.atlassian.com/t5/Jira-questions/Behavior-Script-to-show-a-text-field-amp-mandatory-upon-any/qaq-p/1884010#M174889). For this use case, the Checkbox value selected will display a text field and set it to required or hide it and set it to not required.

You will need to modify the code according to your requirements.

Please go through the solution provided and let me know how it goes.

Thank you and Kind regards,

Ram

comments: author: Digvijay Singh Gehlot
body: Hi [@Ram Kumar Aravindakshan _Adaptavist_](/t5/user/viewprofilepage/user-id/2988015)

Thank you for sharing that related Community page.

I followed the post thoroughly and tried to run the below code on Jira data center (9.12.12) at my end:

import com.atlassian.jira.component.ComponentAccessor  
import com.onresolve.jira.groovy.user.FieldBehaviours  
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours

def sampleCheckbox = getFieldById(fieldChanged)  
def sampleCheckboxValue = sampleCheckbox.value.toString()

def sampleText_1 = getFieldByName('Alpha ID')  
def sampleText_1 = getFieldByName('Beta ID')  
def sampleText_2 = getFieldByName('Gamma ID')

sampleText_1.hidden = true  
sampleText_2.hidden = true  
sampleText_3.hidden = true

def customFieldManager = ComponentAccessor.customFieldManager  
def oldSampleCheckBox = customFieldManager.getCustomFieldObjectsByName('Sample Checkbox').first()

if (underlyingIssue) {  
if (underlyingIssue.getCustomFieldValue(oldSampleCheckBox).toString() != sampleCheckboxValue) {  
sampleText_1.hidden = false  
}  
if (underlyingIssue.getCustomFieldValue(oldSampleCheckBox).toString() != sampleCheckboxValue) {  
sampleText_2.hidden = false  
}

if (underlyingIssue.getCustomFieldValue(oldSampleCheckBox).toString() != sampleCheckboxValue) {  
sampleText_3.hidden = false  
}  
}

____________________________________________________________________________________________

However, I observed that:

1) On selecting the option "Alpha" only in Sample Checkbox, all three Text fields are appearing: Alpha ID, Beta ID, Gamma ID.

2) On selecting the option "Beta" only in Sample Checkbox, again two Text fields are appearing: Alpha ID, Beta ID, Gamma ID.

3) On selecting the option "Alpha" and "Beta" only in Sample Checkbox, again two Text fields are appearing: Alpha ID, Beta ID, Gamma ID.

Can you further help me to refine my above code so that user should only see Alpha ID (Text field) if he selects Checkbox option "Alpha" or if user selects multiple Checkbox options: "Alpha" and "Beta", then only two Text fields should appear: Alpha ID, Beta ID (Gamma ID should be hidden if user has not selected "Gamma" as Checkbox option)?

Thanks


author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Digvijay Singh Gehlot](/t5/user/viewprofilepage/user-id/5431110)

So for your requirement, you need to modify the code accordingly. The sample I have provided was for an Issue that has already been created and will not work on the Create screen, i.e. the underlyingIssue variable is only accessible once the issue has been created.

Below is a sample working code for your reference:-  

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

@BaseScript FieldBehaviours behaviours
def sampleCheckbox = getFieldById(fieldChanged)
def sampleCheckboxValue = sampleCheckbox.value.toString()

def alphaID = getFieldByName('Alpha ID')
def betaID = getFieldByName('Beta ID')
def gammaId = getFieldByName('Gamma ID')

alphaID.hidden = true
alphaID.required = false

betaID.hidden = true
betaID.required = false

gammaId.hidden = true
gammaId.required = false

if (sampleCheckboxValue == 'Option1') {
    alphaID.hidden = false
    alphaID.required = true
} else if (sampleCheckboxValue == 'Option2') {
    betaID.hidden = false
    betaID.required = true
} else if (sampleCheckboxValue == 'Option3') {
    gammaId.hidden = false
    gammaId.required = true
}
```

Below is a screenshot of the Behaviour configuration for your reference:-

![behaviour_config.png](https://community.atlassian.com/t5/image/serverpage/image-id/344241iA80E7FAE289B7861/image-size/large?v=v2&px=999 "behaviour_config.png")

I hope this helps solve your question. :-)  

Thank you and Kind regards,  
Ram


author: Digvijay Singh Gehlot
body: Hi [@Ram Kumar Aravindakshan _Adaptavist_](/t5/user/viewprofilepage/user-id/2988015)

<br />

I am showing the required custom fields on Edit Screen only.

So, I run the below:

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

@BaseScript FieldBehaviours behaviours  
def sampleCheckbox = getFieldById(fieldChanged)  
def sampleCheckboxValue = sampleCheckbox.value.toString()

def alphaID = getFieldByName('Alpha ID')  
def betaID = getFieldByName('Beta ID')  
def gammaId = getFieldByName('Gamma ID')

alphaID.hidden = true  
betaID.hidden = true  
gammaId.hidden = true

if (sampleCheckboxValue == 'Option 1') {  
alphaID.hidden = false  
} else if (sampleCheckboxValue == 'Option 2') {  
betaID.hidden = false  
} else if (sampleCheckboxValue == 'Option 3') {  
gammaId.hidden = false  
}

The result is:  
1) When I select only "Alpha" as checkbox option, I am able to see "Alpha ID" text field.  
2) When I select only "Beta" as checkbox option, I am able to see "Beta ID" text field.![Edit Screen - Option 1 selected.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/344304i5886021E8EA6304D/image-size/large?v=v2&px=999 "Edit Screen - Option 1 selected.PNG")

3) But when I select "Alpha" and "Beta" only as checkbox options, I am not able to see "Alpha ID" and "Beta ID" text fields at the same time.![Edit Screen - Option 1 & Option 2 selected.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/344305i89EDC369D7CC81C0/image-size/large?v=v2&px=999 "Edit Screen - Option 1 & Option 2 selected.PNG")

May you guide further with the code?

Thanks


author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Digvijay Singh Gehlot](/t5/user/viewprofilepage/user-id/5431110)

This is pretty straightforward.

If you intend to select both Option 1 and Option 2, you must add additional if/else conditions for it accordingly.

In the example I have provided, I have only included one option for Option 1 and one for Option 2.

You must modify your code to something like:-

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

@BaseScript FieldBehaviours behaviours
def sampleCheckbox = getFieldById(fieldChanged)
def sampleCheckboxValue = sampleCheckbox.value.toString()

def alphaID = getFieldByName('Alpha ID')
def betaID = getFieldByName('Beta ID')
def gammaId = getFieldByName('Gamma ID')

alphaID.hidden = true
alphaID.required = false

betaID.hidden = true
betaID.required = false

gammaId.hidden = true
gammaId.required = false

if (sampleCheckboxValue == 'Option1') {
    alphaID.hidden = false
    alphaID.required = true
} else if (sampleCheckboxValue == 'Option2') {
    betaID.hidden = false
    betaID.required = true
} else if (sampleCheckboxValue == 'Option3') {
    gammaId.hidden = false
    gammaId.required = true
} else if (sampleCheckboxValue == ['Option1', 'Option2']) {
     alphaID.hidden = false
     alphaID.required = true
     gammaId.hidden = false
     gammaId.required = true
}
```

Please note that the sample working code above is not 100% exact to your environment. Hence, you must make the required modifications.

I hope this helps to answer your question. :-)  

Thank you and Kind regards,  
Ram


author: Digvijay Singh Gehlot
body: Hi [@Ram Kumar Aravindakshan _Adaptavist_](/t5/user/viewprofilepage/user-id/2988015)

I followed the same as you suggested, but expected result is not reflecting on Edit Screen. Please find the below code:

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

@BaseScript FieldBehaviours behaviours

def sampleCheckbox = getFieldById(fieldChanged)  
def sampleCheckboxValue = sampleCheckbox.value.toString()

def alphaID = getFieldByName('Alpha ID')  
def betaID = getFieldByName('Beta ID')  
def gammaId = getFieldByName('Gamma ID')

alphaID.hidden = true  
betaID.hidden = true  
gammaId.hidden = true

if (sampleCheckboxValue == 'Option 1') {  
alphaID.hidden = false  
} else if (sampleCheckboxValue == 'Option 2') {  
betaID.hidden = false  
} else if (sampleCheckboxValue == 'Option 3') {  
gammaId.hidden = false  
}  
else if (sampleCheckboxValue == \['Option1', 'Option2'\]) {  
alphaID.hidden = false  
betaID.hidden = false  
}  
else if (sampleCheckboxValue == \['Option2', 'Option3'\]) {  
betaID.hidden = false  
gammaId.hidden = false  
}  
else if (sampleCheckboxValue == \['Option1', 'Option3'\]) {  
alphaID.hidden = false  
gammaId.hidden = false  
}  
else if (sampleCheckboxValue == \['Option1', 'Option2', 'Option3'\]) {  
alphaID.hidden = false  
betaID.hidden = false  
gammaId.hidden = false  
}  
- Two Text fields are NOT showing when user selects two checkbox options?  
Thanks


author: Digvijay Singh Gehlot
body: Hi [@Ram Kumar Aravindakshan _Adaptavist_](/t5/user/viewprofilepage/user-id/2988015)

I also tried to write the below code but no success. I am not getting any errors but the expected result is not displaying on Edit Screen:

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

@BaseScript FieldBehaviours behaviours

FormField sampleCheckbox = getFieldByName("Sample Checkbox")  
FormField alphaID = getFieldByName("Alpha ID")  
FormField betaID = getFieldByName("Beta ID")  
FormField gammaID = getFieldByName("Gamma ID")

def sampleCheckboxValue = getFieldByName("Sample Checkbox").value  
def chosenList = \[\]

alphaID.setHidden(true)  
betaID.setHidden(true)  
gammaID.setHidden(true)

if (sampleCheckboxValue in String) {

chosenList.add(sampleCheckboxValue)

}

else if (sampleCheckboxValue in ArrayList) {

chosenList.addAll(sampleCheckboxValue)

}

if (chosenList == \['Option 1'\]) {

alphaID.setHidden(false)

}

else if (chosenList == \['Option 2'\]) {

betaID.setHidden(false)

}

else if (chosenList == \['Option 3'\]) {

gammaID.setHidden(false)

}

else if (chosenList == \['Option1', 'Option2'\]) {

alphaID.setHidden(false)  
betaID.setHidden(false)

}

else if (chosenList == \['Option2', 'Option3'\]) {

betaID.setHidden(false)  
gammaID.setHidden(false)

}

else if (chosenList == \['Option1', 'Option3'\]) {

alphaID.setHidden(false)  
gammaID.setHidden(false)

}

else if (chosenList == \['Option1', 'Option2', 'Option3'\]) {

alphaID.setHidden(false)  
betaID.setHidden(false)  
gammaID.setHidden(false)

}

It would be a great help if you could provide a refined code for my use-case.

Thanks


author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Digvijay Singh Gehlot](/t5/user/viewprofilepage/user-id/5431110)

Please share a screenshot of your Behaviour configuration along with a screenshot of the UI to which you are trying to include the Behaviour.

I am looking forward to your feedback.

Thank you and Kind regards,  
Ram


author: Digvijay Singh Gehlot
body: Hi [@Ram Kumar Aravindakshan _Adaptavist_](/t5/user/viewprofilepage/user-id/2988015)

Please find the below screenshots:

1) Behavior screenshots:

![1.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/344452i651316676930A189/image-size/large?v=v2&px=999 "1.PNG")![2.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/344453iCCF0AD83FFF2129B/image-size/large?v=v2&px=999 "2.PNG")

2) Behavior applied to Edit Screen:

![3.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/344455iE42329B28CA994A6/image-size/large?v=v2&px=999 "3.PNG")![4.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/344456i6BE3554C9D22FF78/image-size/large?v=v2&px=999 "4.PNG")

So, selecting one Checkbox option, I am able to see the associated Text field

But on selecting two Checkbox options, I am unable to see two Text fields associated with the Checkbox options.

Note: I am using Jira version (9.12.12) of Jira QA environment.

Looking forward to hearing from you.

Thanks


author: Digvijay Singh Gehlot
body: Hi [@Ram Kumar Aravindakshan _Adaptavist_](/t5/user/viewprofilepage/user-id/2988015)

I tried to look more into the code and checked on online groovy editor and found that there may be some Groovy Console errors as below, I hope this will help to resolve the issue.

```
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:/Main.groovy: 1: unable to resolve class com.onresolve.jira.groovy.user.FieldBehaviours @ line 1, column 1.   import com.onresolve.jira.groovy.user.FieldBehaviours   ^1 error
```


author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Digvijay Singh Gehlot](/t5/user/viewprofilepage/user-id/5431110)

After going through your code, I have observed a couple of mistakes.

Firstly, why do you initialise the field by name instead of getFieldById(fieldChanged), as I have demonstrated in my sample code?

The whole point of the fieldChanged is to ensure that the Behaviour will only trigger when the change to the specific field the Server-Side Behaviour has been configured for is updated.

Secondly, why are you using FormField to initiate your variables? Please follow the steps provided in my code, i.e., declare it with the def variable.

Also, why are you doing this:-

```
...
...
else if (sampleCheckboxValue in ArrayList)
```

This is of no use and will not work as expected.

Please follow the structure that I have provided and only change the if/else conditions according to the value of the Checkbox that has been selected.

You must correct your code to what is shown below for it to work a expected.  

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

@BaseScript FieldBehaviours behaviours
def sampleCheckbox = getFieldById(fieldChanged)
def sampleCheckboxValue = sampleCheckbox.value

def alphaID = getFieldByName('Alpha ID')
def betaID = getFieldByName('Beta ID')
def gammaID = getFieldByName('Gamma ID')

alphaID.hidden = true
betaID.hidden = true
gammaID.hidden = true


if (sampleCheckboxValue == 'Option 1') {
    alphaID.hidden = false
} else if (sampleCheckboxValue == 'Option 2') {
    betaID.hidden = false
} else if (sampleCheckboxValue == 'Option 3') {
    gammaID.hidden = false
} else if (sampleCheckboxValue == ['Option 1', 'Option 2']) {
    alphaID.hidden = false
    betaID.hidden = false
} else if (sampleCheckboxValue == ['Option 2', 'Option 3']) {
    betaID.hidden = false
    gammaID.hidden = false
} else if (sampleCheckboxValue == ['Option 1', 'Option 3']) {
    alphaID.hidden = false
    gammaID.hidden = false
} else if (sampleCheckboxValue == ['Option 1', 'Option 2', 'Option 3']) {
    alphaID.hidden = false
    betaID.hidden = false
    gammaID.hidden = false
}
```

Please note that the sample working code above is not 100% exact to your environment. Hence, you must make the required modifications.

I am looking forward to your feedback.

Thank you and Kind regards,  
Ram


author: Digvijay Singh Gehlot
body: Hi [@Ram Kumar Aravindakshan _Adaptavist_](/t5/user/viewprofilepage/user-id/2988015)

Thank you so much for reviewing my code and providing the valuable insights.

I have refined my code again as you guided and now, it is working fine as expected. I'll make a note of those points for my future reference.

Best,

Digvijay


author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Digvijay Singh Gehlot](/t5/user/viewprofilepage/user-id/5431110)

In your last comment, you asked
> I am applying if/else condition for 10 Checkbox options, and it is getting too complex to write the code for some many combinations.
>
> Is there a way, we can use Select List (Multi-Choose) instead of multiple checkbox options regarding my use-case?
>
> Can you also provide more guidance with the code on how to hide/ show text fields based on Select List (Multi-Choose) options?

The simple answer is that it depends.

If you intend for all possible options from the 10 checkboxes to be validated then no, there is no simple way to do it.

I suggest adding some comments for your conditions so you can keep track of what the code is doing.

Alternatively, if you want to do it all in one line, you could try using the [ternary operator](https://www.geeksforgeeks.org/java-ternary-operator-with-examples/).

Thank you and Kind regards,  
Ram


author: Digvijay Singh Gehlot
body: Hi [@Ram Kumar Aravindakshan _Adaptavist_](/t5/user/viewprofilepage/user-id/2988015)

I am applying if/else condition for 10 Checkbox options, and it is getting too complex to write the code for some many combinations.

Is there a way, we can use Select List (Multi-Choose) instead of multiple checkbox options regarding my use-case?

Can you also provide more guidance with the code on how to hide/ show text fields based on Select List (Multi-Choose) options?

Looking forward to hearing from you soon.

Thanks


author: Digvijay Singh Gehlot
body: Hi [@Ram Kumar Aravindakshan _Adaptavist_](/t5/user/viewprofilepage/user-id/2988015)

May you guide me further with the above use-case by using scriptrunner?

Looking forward to hearing from you soon.

Thanks


Tags: behaviours

checkbox

checklist

data-center

jira-data-center

jmwe

jsu

scriptrunner