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:
However as soon as I add another option to this multi-select, the field "FreeRounds type" disappears:
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:
and the Behaviour loses its functionality entirely. The field is never hidden:
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:
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
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:
Field hidden when option selected:
However, selecting multiple options containing "FreeRounds type", it works again:
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
Cheers,
Alex
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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:
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hey @Alex
I concur that the customfield_12103 is a Select List (multiple choice)
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. When the option is selected, the field disappears, which it shouldn't, and the description appears.
3. Selecting multiple options, makes the field visible again, and the description remains there.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
2. "FreeRounds type" option selected, the field appears
3. Selecting multiple options including FreeRunds type, the field appears
4. Selecting any other option except FreeRounds type, the field stays hidden.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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:
And now, when 2 or more options are selected:
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:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.