Jira - How to clean a field with script ?

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

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
}

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.

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

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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published yesterday in Jira Software

How large do you think Jira Software can grow?

Hi Atlassian Community! My name is Shana, and I’m on the Jira Software team. One of the many reasons this Community exists is to connect you to others on similar product journeys or with comparabl...

271 views 4 9
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you