how to clear custom field value when you edit another custom field ?

Hi,

I'm looking for a solution which would let me to clear custom fields value when I edit and select anoher custom field value.

For example, I have a Radio buttons field named Run Impacts. Possible values are : No/positive/negative.

If I first select positive or negative, some other custom fields (A, B, C let's say) needed to be completed display (via behaviours plugin). But if I edit the value and finally select "No", I would need that the value of all the A, B, C fields  be cleared.

I think I should create a custom script listener but i don't know how to build the script.

Any ideas?

Thanks for your help

Melissa

3 answers

1 accepted

Hi Melissa,

You can add a listener, as you described admin > script Listeners > Custom Listener, assign the project / projects you want and for the events you should select Issue Updated (therefore every time there is a change at the radio button custom field the listener will get triggered). For inline script copy / paste the script below

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

Issue issue = event.issue

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def customField2 = customFieldManager.getCustomFieldObjectByName("radioBtn")
LazyLoadedOption radioBtnOption = (LazyLoadedOption) issue.getCustomFieldValue(customField2)

def changeHolder = new DefaultIssueChangeHolder();
def customField4 =  customFieldManager.getCustomFieldObjects(issue).find {it.name == "textfield"}


if (radioBtnOption?.getValue() == "No") {
    customField4.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField4), ""),changeHolder)
}

What it does: If the value of the custom field "radioBtn" is "No" then it updates the value of the custom field with name "textfield" with an empty string. This applies only for text custom fields. Hope that helps.

Edit: Now with Kristian's script you have two options. Via a behaviour or a Listener. Choose which suits your needs. 

Kind regards

thanks to you both Thanos and Kristian

Hi Mel,

You can do this with Behaviours. I have enclosed a sample script below which checks the value in Radio Button A and when option 1 is selected clears the value in Radio Button B.

Note that by default JIRA excludes the none option from the options map so we have to include this before we can set the value to no value selected (none).

The documentation here shows example code of how you could set a radio button option if required.

// Get the custom fields by Name
def radioA = getFieldByName("Radio A")
def radioB = getFieldByName("Radio B")
// get the value of Radio A
String radioAVal = radioA.getValue()
//Define the option for none as Jira does not include this in the radio fields map by default
 Map radioBFieldOptions = [:]
    radioBFieldOptions.put ("-1", "None")
// If the value selected in Radio Button A is 1
if (radioAVal == "1") {
    // Set Radio Button B to None to clear its value
    radioB.setFormValue(-1)
}

I hope this helps

Thanks

Kristian

You could action this with a script as a post-function action if the changes are only going to be made during transitions. But if it could be altered using the edit functionality you do indeed need to use a listener. 

One option for you is to use ScriptRunner and you may want to review the documentation at https://scriptrunner.adaptavist.com/latest/jira/listeners.html for how listeners work and https://scriptrunner.adaptavist.com/latest/jira/recipes/workflow/postfunctions/set-issue-attributes.html for an example of how to set issue attributes.

Hope this helps.

Hi Phill, Indeed my problem concerns the edit functionnality. Thanks for the doc. i'm going to go through it

@Thanos Batagiannis [Adaptavist] can you help here? Have you got an example script that you could share with @mel vega

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

Mission-critical battery manufacturer fulfills FAA software requirements with Commit Policy Plugin

EaglePicher Technologies is a leading manufacturer of battery systems for diverse industries like defense, aviation, space or medical. As they operate in highly regulated industries, keeping a clear ...

127 views 0 2
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