Hello,
Context: I have a radio button which add the values "Orange" and "Green" in the field "component". For that, I'm using a script in "behaviours" :
getFieldById(COMPONENTS).setFormValue(components.findAll { it.name in ["Orange", "Vert"] }*.id)
Problem: The field "component" stay filled when I choose another option with my radio button.
What I want: I'd like to know what line of code should I use to erase the content of the "component" field before I fill it.
Thanks for your help.
The simplest way to erase the field would be
def componentsField = getFieldById(COMPONENTS) componentsField.setFormValue([])
Be forewarned, that will erase all components, including those set by the user and ones you aren't interested in changing. For example, let's say you also had the components Apple and Pie. You wouldn't necessarily want to wipe those on a change of a radio button. If you need, you could get the existing values, and use those in each set operation.
def existingValues = componentsField.getValue() if (radioButtonCustomField == "whatever") { componentsField.setFormValue(components.findAll { it.name in ["Orange", "Vert"] || it.id in existingValues }*.id) } else { componentsField.setFormValue(existingValues.removeAll(it.name in ["Orange", "Vert"])) }
You may or may not need to do that, depending on your context.
Hello jcarter!
I tried the script, but the values "Orange" and "Green" doesn't seems to disappear in my creation screen when I click on an other value than "Blue" (with the radio button).
Is there an error on this script ??
------------------------------------------------------------------------------------------------------------------------------ import com.atlassian.jira.component.ComponentAccessor import static com.atlassian.jira.issue.IssueFieldConstants.* // set Components def projectComponentManager = ComponentAccessor.getProjectComponentManager() def components = projectComponentManager.findAllForProject(issueContext.projectObject.id) def componentsField = getFieldById(COMPONENTS) def existingValues = componentsField.getValue() FormField radio = getFieldByName("Button_Radio") if(radio.getValue() == "Blue") // click on the “Blue” value in the "Button_Radio" { componentsField.setFormValue(components.findAll { it.name in ["Orange", "Green"] || it.id in existingValues }*.id) //set “Orange” and “Green” in the component field } else { componentsField.setFormValue(existingValues.removeAll(it.name in ["Orange", "Green"])) //remove “Orange” and “Green” in the component field }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The Groovy collection method removeAll
actually returns a boolean and mutates the original collection. So you'll need to either replace it with a findAll and negate your condition
existingValues.findAll{!(it.name in ["Orange", "Green"])}
or do the removeAll on a separate line. Don't for get to add the *.id aggregator either, as setFormValue for components takes a list of IDs, not component objects.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello jcarter, I finaly manage the situation with your two solutions.
But the solution doesn't work with an user account (only with my administrator account). That's why it wasn't working when I answered you. Have you an idea of what could I do for that ??
Thanks again for your involvement
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.