Forums

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

Conditionally show a custom field based on a selected option in multi-select field.

Masa Janezic October 18, 2021

Hi, 

I'm trying to achieve that a custom field only appears when a multi-select field contains a certain option. For that, I'm using Behaviours in Scriptrunner plugin from Adaptavist. 

I have used Behaviours before with a single select and tried using the same groovy script here but it doesn't do what I need. This is the script:

//customfield_12103 = multi-select
//customfield_12100 = checkbox field
//I want the checkbox field to apear only if the multi-select field contains "FreeRounds type" option.
def dropDown = getFieldById("customfield_12103")
def conditionA = getFieldById("customfield_12100")

log.debug("dropdown value" + dropDown.getValue())

if (dropDown.getValue() == "FreeRounds type") {
conditionA.setHidden(false);
conditionA.setRequired(false);
} else {
conditionA.setHidden(true);
conditionA.setRequired(false);
}

What it does, is that if I only select "FreeRounds type" option in my multi-select it works fine and the field of the same name appears:

1.png

However as soon as I add another option to this multi-select, the field "FreeRounds type" disappears:

2.png

I found that changing == to .contains would work so I changed the line 6 to:

if (dropDown.getValue().contains("FreeRounds type")) {

but that returns an error:

3.png

and the Behaviour loses its functionality entirely. The field is never hidden: 

4.png

I have searched around and couldn't find a working example for my case anywhere. I am no coder, can anyone please help me with this groovy script for multi-select field, please?

We're using:

  • Jira Software v8.13.9
  • Scriptrunner v6.30.2

1 answer

1 accepted

1 vote
Answer accepted
Alex
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 18, 2021

Hi @Masa Janezic, and welcome to the community!

 

I think this modification might do the job:

//customfield_12103 = multi-select
//customfield_12100 = checkbox field
//I want the checkbox field to apear only if the multi-select field contains "FreeRounds type" option.
def dropDown = getFieldById("customfield_12103")
def conditionA = getFieldById("customfield_12100")

log.debug("dropdown value" + dropDown.getValue())

String[] options = dropDown.getValue() as String[]

if (options.contains("FreeRounds type") {
conditionA.setHidden(false);
conditionA.setRequired(false);
} else {
conditionA.setHidden(true);
conditionA.setRequired(false);
}

 In summary, cast the getValue() return to an array of String and verify that variable e\woth a  contains() method.

I hope it helps. If this answer helps solve the problem, please come back and mark this answer as solved to help other community members with the same challenge. If not, you are welcome to share your solution as well.

Cheers,

Alex

Masa Janezic October 19, 2021

Thank you, Alex. 

There was an error but I found it and fixed it by adding a ) to the if line.

//customfield_12103 = multi-select
//customfield_12100 = checkbox field
//I want the checkbox field to apear only if the multi-select field contains "FreeRounds type" option.
def dropDown = getFieldById("customfield_12103")
def conditionA = getFieldById("customfield_12100")

log.debug("dropdown value" + dropDown.getValue())

String[] options = dropDown.getValue() as String[]

if (options.contains("FreeRounds type")) {
conditionA.setHidden(false);
conditionA.setRequired(false);
} else {
conditionA.setHidden(true);
conditionA.setRequired(false);
}

Sadly even after adding the bracket, this still doesn't work properly. Funny, because what happens now is that halfway it somehow works backwards. Meaning that FreeRounds type field isn't hidden until the option "FreeRounds type" is selected.

Option not selected, filed not hidden:

1.png

Field hidden when option selected:

2.png

However, selecting multiple options containing "FreeRounds type", it works again:

3.png
To sum up, this works only halfway. Can we somehow get it to work in both cases? That is in working case of selecting multiple options, including "FreeRounds type" as well as in the event that only "FreeRounds type" is selected?

Thank you. Your help is greatly appreciated.

Alex
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 19, 2021

Hey @Masa Janezic 

I am glad you've got some progress on having the behaviour script working for your use case.

The reason why it does not work when the page first loads is that you need to ensure that this script is triggered by adding it to the Initialiser section of the behaviour, along with the same script in the field section.

Screen Shot 2021-10-19 at 10.02.50 AM.png

 

Cheers,

Alex

Masa Janezic October 20, 2021

Hey, @Alex thanks again for your help.

I've added the script as an initializer too but it didn't change anything. The field FreeRounds type is shown as the page loads and disappears when "FreeRounds type" option is selected as a single option in External Wallet Features field. It of course reappears again when there are other options selected in the field. 

The field FreeRounds type should appear whenever a "FreeRounds type" option is selected in the External Wallet Features field. As such it shouldn't be seen when the "FreeRounds type" isn't selected.

Is there anything else we could try?

 

Thank you,

Maša

Alex
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 20, 2021

Hi @Masa Janezic 

It's funny, but I have a pretty similar script on a test environment, and it works as expected. I changed the custom fields id's to use your example, and it still works.

//customfield_12103 = multi-select
//customfield_12100 = checkbox field
//I want the checkbox field to apear only if the multi-select field contains "FreeRounds type" option.
def dropDown = getFieldById("customfield_13116")
def conditionA = getFieldById("customfield_14416")


String[] options = dropDown.getValue() as String[]

if (options.contains("Other")) {
conditionA.setHidden(false);
conditionA.setRequired(false);
} else {
conditionA.setHidden(true);
conditionA.setRequired(false);
}

// Here I force the description to be the option list for testing
dropDown.setDescription("Options = " + options)

One difference that I noticed is that the field that I am using Type of Change is a Select List (multiple choice), as you can see below:

Screen Shot 2021-10-20 at 10.57.01 PM.png

The field Automated Change appears only if Other is selected regardless if any other option is selected or not.

Can you review what custom field type is the customfield_12103 ? Also, if you employ the content of whatever variable you'd like to test to the description of the field, it's easier to figure out the content stored in that field.

Ex: 

dropDown.setDescription("Options = " + options)

Cheers,

Alex 

Masa Janezic October 21, 2021

Hey @Alex 

I concur that the customfield_12103 is a Select List (multiple choice) 

1.png

And yes, your example is exactly what I'm trying to achieve. 

I added the description in both, the initializer and in the field as follows and it appears in the description.

//customfield_12103 = multi-select
//customfield_12100 = checkbox field
//I want the checkbox field to apear only if the multi-select field contains "FreeRounds type" option.
def dropDown = getFieldById("customfield_12103")
def conditionA = getFieldById("customfield_12100")
dropDown.setDescription("Options = " + "[FreeRounds type]")

log.debug("dropdown value" + dropDown.getValue())

String[] options = dropDown.getValue() as String[]

if (options.contains("FreeRounds type")) {
conditionA.setHidden(false);
conditionA.setRequired(false);
} else {
conditionA.setHidden(true);
conditionA.setRequired(false);
}

The functionality stays the same. 

1. The field is visible and there is no description.

2.png

2. When the option is selected, the field disappears, which it shouldn't, and the description appears.3.png

3. Selecting multiple options, makes the field visible again, and the description remains there.

4.png

I'm not sure where I could begin to look for the issue. It would be helpful if you could point me to the issue. 

 

Thanks,

Masa

Masa Janezic October 21, 2021

Hey @Alex 

Good news! We've managed to find the issue in-house. One of our admins enabled the dark feature multiselect.frother.renderer, which I wasn't aware of. 

Some of our Multi-Select fields are enormous. They contain many options, and the dark feature renders it as a picker field. This is why my multi-select field looked different to yours. With the dark feature enabled, it's a lot easier for our users to select the desired options from a multi-select field. Turns out disabling it made the Behaviour work properly as you suggested. I'm marking your answer as accepted.

Here is the code in action, working as expected:

1. No option selected, field is hidden

1.png

2. "FreeRounds type" option selected, the field appears

2.png

3. Selecting multiple options including FreeRunds type, the field appears

3.png

4. Selecting any other option except FreeRounds type, the field stays hidden.

4.png

I was wondering if you would know of a workaround to somehow keep this dark feature enabled as well as have this Behaviour working properly. Or if you are aware of any other way to make this work as expected, along with the dark feature being enabled. 

Thanks again for all your effort. It did solve the initial problem :D

 

Hope you have a wonderful day,

Masa

Alex
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, 2021

Hey @Masa Janezic 

 

Happy to hear that you figured out the mystery. Even happy that I also learned a piece of something from this, thank you!

I enabled the same dark feature on my Jira and it all makes sense now.

I am still using the description trick I mentioned before. See how Jira stores the information when 1 option is selected:

Screen Shot 2021-10-21 at 10.57.23 AM.png

And now, when 2 or more options are selected:

Screen Shot 2021-10-21 at 10.59.10 AM.png

I managed to get around this by changing how the options list stores the values:

//customfield_12103 = multi-select
//customfield_12100 = checkbox field
//I want the checkbox field to apear only if the multi-select field contains "FreeRounds type" option.
def dropDown = getFieldById("customfield_13116")
def conditionA = getFieldById("customfield_14416")


//String[] options = dropDown.getValue() as String[]
def options = dropDown.getValue().toString()


if (options.contains("Other")) {
conditionA.setHidden(false);
conditionA.setRequired(false);
} else {
conditionA.setHidden(true);
conditionA.setRequired(false);
}

dropDown.setDescription("Options = " + options)

The behaviour is now as expected again:

  • Nothing selected:

Screen Shot 2021-10-21 at 11.25.37 AM.png

  • Other selected:

Screen Shot 2021-10-21 at 11.25.11 AM.png

  • Other and more options selected:

Screen Shot 2021-10-21 at 11.26.31 AM.png

Like Masa Janezic likes this
Masa Janezic October 22, 2021

This is awesome, @Alex 

I can confirm that this works as expected now. Even with the dark feature enabled. I used the above script with my custom field IDs and managed to implement the same solution on one other field of the same type - multi select custom field. Paint me amazed by your quick and clever responses. Thanks again!

 

Stay a champ,

Maša

Like Alex likes this
Ajinkya Paradkar January 6, 2022

Hi , 

I went through the above discussion , I have a similar requirement where we have a Cascading field which when selected as option "A" , should display a new custom field on the screen.

I tried the above code but it doesn't work , any suggestions would be helpful ?

 

Thanks

Ajinkya

Masa Janezic January 10, 2022

Hi @Ajinkya Paradkar ,

 

I don't think you can use the same script as I am not using cascading fields in Behaviours. Perhaps try finding a script which is designed for a cascading filed instead.

Not sure, but maybe try reviewing this thread:
https://community.atlassian.com/t5/Jira-questions/With-Scriptrunner-Behaviour-hide-a-field-based-on-the-values-of/qaq-p/1717301

Best of luck!

 

Masa

Suggest an answer

Log in or Sign up to answer