Scriptrunner Behaviours - set different options and hide custom field based on another

Nikita Bakanov August 27, 2018

Hi there!


I'm trying to figure out of what's wrong with my Behaviours script.
The main idea is that I have my single select field with three options and I would like to manipulate other field(s) depending of these options.
But I faced with a strange thing - I can set different options for other field (in case AAA and BBB) and I can hide it, but it doesn't become visible again.

So, here is my script for the custom single select field:

import com.atlassian.jira.component.ComponentAccessor

def singleSelect = getFieldById(getFieldChanged())
def optionsManager = ComponentAccessor.getOptionsManager()

def cf = getFieldById("customfield_16113")
def cfField = customFieldManager.getCustomFieldObject(cf.getFieldId())
def cfConfig = cfField.getRelevantConfig(getIssueContext())
def cfOptions = optionsManager.getOptions(cfConfig)

cf.setHidden(false)

def selectedOption = singleSelect.getValue() as String
def ccc = selectedOption == "CCC"

switch (selectedOption) {
    case "AAA":
     def cfA = cfOptions.findAll { it.value in ["A","B","C","D"] }.collectEntries { [ (it.optionId.toString()) : it.value ] }
     cf.setFieldOptions(cfA)
     break
    case "BBB":
     def cfB = cfOptions.findAll { it.value in ["B","D","H","K"] }.collectEntries { [ (it.optionId.toString()) : it.value ] }
     cf.setFieldOptions(cfB)
     break
    case "CCC":
     cf.setHidden(true)
     break
}
cf.setRequired(! ccc)

In general I would like to show different sets of options for my customfield depending on value of singleSelect field and hide customfield at all in the last case. But the issue is that after hiding it doesn't become visible again if I change the value of singleSelect field.
Is that something connected to the issue / field context?

Please point me to the documentation or something.

Thanks,

2 answers

1 vote
Mark Markov
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.
August 27, 2018

Hello @Nikita Bakanov

You re missed one little point. If you want to make field visible after you hide it, you should set it visible in cases it should

import com.atlassian.jira.component.ComponentAccessor

def singleSelect = getFieldById(getFieldChanged())
def optionsManager = ComponentAccessor.getOptionsManager()

def cf = getFieldById("customfield_16113")
def cfField = customFieldManager.getCustomFieldObject(cf.getFieldId())
def cfConfig = cfField.getRelevantConfig(getIssueContext())
def cfOptions = optionsManager.getOptions(cfConfig)

cf.setHidden(false)

def selectedOption = singleSelect.getValue() as String
def ccc = selectedOption == "CCC"

switch (selectedOption) {
case "AAA":
def cfA = cfOptions.findAll { it.value in ["A","B","C","D"] }.collectEntries { [ (it.optionId.toString()) : it.value ] }
cf.setHidden(false)
cf.setFieldOptions(cfA)
break
case "BBB":
def cfB = cfOptions.findAll { it.value in ["B","D","H","K"] }.collectEntries { [ (it.optionId.toString()) : it.value ] }
cf.setHidden(false)
cf.setFieldOptions(cfB)
break
case "CCC":
cf.setHidden(true)
break
}
cf.setRequired(! ccc)
Nikita Bakanov August 27, 2018

Thank you @Mark Markov!
I supposed that this script would be re-initiated on field change, that is why I placed cf.setHidden(false) before my switch statement.

Unfortunately your suggestion doesn't help me much as the fields behave the same way - after select CCC option customfield becomes invisible, but does not appear back when I choose another value from single select.

It is also interesting that if I comment the lines which define options for AAA and BBB cases leaving setHide lines only, then everything works as expected.

Could it be that there is no way to set the options and determine the visibility as well? Or I missed something important

Like Daniel Morris likes this
Daniel Morris April 22, 2021

I know this is an old post but i've just hit this same issue in scriptrunner. It seems to be a genuine bug as my code isn't as complex as above. Basically if you use the method .setFieldOptions() to set a field that the same script hides and shows that field will get hidden correctly but will not be shown again when calling .setHidden(false). If you comment out either the setHidden calls or the setFieldOptions call the code works correctly.

Jonathan V. August 4, 2022

Hi, I am following-up on this as I am also having a similar issue.

Whenever I have setFieldOptions and setHidden, it appears as if the code ignores the setHidden value.

Was this ever addressed or is there a workaround to allow both?

0 votes
gvidali February 6, 2019

Hi,

I found your sample superuseful to get a subcategory select list updated according to the selection of a category select list.

Now I would like to update also another custom field which is a user picker.

I was able to update it with JS but I drop the JS solution due to its unpredictable behaviour and soon deprecation.

Below is what I'm trying but I get a variable issue not declared.

What is a lean way to simply change a user picker custom field ?

 

UPDATE: solved with 

 

def userToNotify = getFieldById("customfield_10000")  //single user picker
userToNotify.setFormValue("gabriele")
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.user.util.DefaultUserManager
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.user.util.UserManager

IssueManager issueManager = ComponentAccessor.getIssueManager();
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField CF = customFieldManager.getCustomFieldObjectByName("User to Notify");
def customUserToNotify = "gabriele";
issue.setCustomFieldValue(CF, customUserToNotify );

 

Suggest an answer

Log in or Sign up to answer