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

mel vega January 7, 2016

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

1 vote
Answer accepted
Thanos Batagiannis _Adaptavist_
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.
January 7, 2016

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

mel vega January 10, 2016

thanks to you both Thanos and Kristian

2 votes
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 7, 2016

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

0 votes
Phill Fox
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 7, 2016

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.

mel vega January 7, 2016

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

Phill Fox
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 7, 2016

@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