Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

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)

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

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.

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
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you