Listener that checks Checkbox when Custom field is edited.

GS1337 September 4, 2019

Hello,

 

like the title suggests, i need a listener that checks a Checkbox if certain custom fields have been edited. Anybody knows something similar that i can use or has some suggestions?  

 

Thank you in advance

2 answers

1 vote
Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 5, 2019

Hi @GS1337 ,

You can add this script in a Issue Updated listener : 

import com.atlassian.jira.component.ComponentAccessor 
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue

def customFieldChanged = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == "Name of your custom field"}

if (customFieldChanged){
def optionsManager = ComponentAccessor.optionsManager

int checkBoxFieldId = 13000
def checkBoxField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(checkBoxFieldId)
def checkboxFieldConfig = checkBoxField.getRelevantConfig(issue)
def options = optionsManager.getOptions(checkboxFieldConfig)
def optionsToSet = options.findAll { it.value in ["option1_to_check", "option2_to_check"] }
def checkBoxFieldValue = issue.getCustomFieldValue(checkBoxField)

checkBoxField.updateValue(null, issue, new ModifiedValue(checkBoxFieldValue, optionsToSet), new DefaultIssueChangeHolder())
}

Antoine

GS1337 September 6, 2019

Thx for your answer, i tried it but it shows an error in last line, when i try to execute it it gives me this error

 

 

 

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2019-09-06 09:44:51,907 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2019-09-06 09:44:51,908 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: <inline script>
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script383.groovy: 17: expecting '}', found '' @ line 17, column 125.
   w DefaultIssueChangeHolder()) 
                                 ^
1 error
GS1337 September 6, 2019

ok i found the mistake, its just a } at the end, but it still doesnt check my Box. I replaced 

"Name of your custom field"

with my custom field name that should be checked for changes

 

I replaced this ID with the ID of my checkbox field

int checkBoxFieldId = 13000

 

And i replaced this with the option of my Custom Field that should be checked like this

it.value in ["My_Option"]

 

What am i doing wrong?

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 6, 2019

You should check the logs to see if the code fails, and if it correctly enters the if condition when you edit the field.

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 11, 2019

Hi @GS1337 ,

Were you able to make this work ?

0 votes
jira guy
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.
September 4, 2019

I see you are trying to use scriptrunner to achieve this. But I think automation for JIRA might be your best bet and is easy to configure. Both plugins offer customfield changed event in their listeners but Automation for JIRA offers the ability to capture specific field easily. You can then update the checkbox field as you see fit using the rule.

Suggest an answer

Log in or Sign up to answer