Jira - How to clean a field with script ?

Rambo July 8, 2016

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.

1 answer

1 accepted

1 vote
Answer accepted
Jonny Carter
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.
July 10, 2016

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.

Rambo July 11, 2016

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
}
Jonny Carter
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.
July 11, 2016

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.

Rambo July 11, 2016

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

Rambo July 12, 2016

Problem solve! It don't work because I used ineternet explorer in my user account. No problem with modzilla. Thank you!

Suggest an answer

Log in or Sign up to answer