ScriptRunner Modifying Checkbox values on transition post-function

Joshua Giffen August 14, 2015

Hi,

I have been looking and experimenting for several days but I cannot find a working solution to change the state of a single customfield checkbox from unchecked to checked or back to unchecked using scriptrunner (Groovy).

I found an almost identical question and I tried modifying the code found here Script runner, how to update custom field that's a select list? 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.fields.config.FieldConfig

def customFieldManager = ComponentManager.getInstance().getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjectByName("TWU - Combined Tax Review Completed")
def fieldConfig = cf.getRelevantConfig(issue)

value = ComponentAccessor.optionsManager.getOptions(fieldConfig)?.find { it.toString() == 'Taxes Completed' }

if (cfValues['Account Type']?.value == 'IRA') {
issue.setCustomFieldValue(cf, value)
}

 

Does anyone have some working code that I could work on customizing?

Thanks

1 answer

1 accepted

1 vote
Answer accepted
JamieA
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.
August 18, 2015

That code looks OK, and it should work providing it's the first post-function in the list.

Joshua Giffen August 25, 2015

Thanks Jamie, It looks like I am running into a problem with the JIRA installation. I tested the code again and found it works too, but there is some other unrelated issue. Thanks!!!!

LaurieC June 21, 2021

Hi @JamieA @Joshua Giffen

I'm trying to do something similar and can't seem to get it to work. It acts like it is working and doesn't through any errors, but it isn't checking the box. 

Any thoughts?

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
import com.atlassian.jira.issue.customfields.manager.OptionsManager

log.setLevel(org.apache.log4j.Level.DEBUG)

final customFieldName = "Test Type"

//Get custom field issue with this name
def customFieldManager = ComponentAccessor.customFieldManager
def customField = customFieldManager.getCustomFieldObjects(issue).findByName(customFieldName)
assert customField: "Could not find custom field with name $customFieldName"

def customFieldVal = issue.getCustomFieldValue(customField) as LazyLoadedOption

log.debug "Test Type is ${customFieldVal}"

//set "Add to Regression Set' as field to be updated-
def optionsManager = ComponentAccessor.getComponent(OptionsManager)

//Update "Add to Regression Set" depending on the custom field value
switch (customFieldVal?.value) {
case "Regression Test":
log.debug "Add to Regression Set checkbox should be checked"
def cf = customFieldManager.getCustomFieldObjectByName("Add to Regression Set")
def fieldConfig = cf.getRelevantConfig(issue)
def options = ComponentAccessor.optionsManager.getOptions(fieldConfig)?.find { it.value == 'Yes' }
issue.setCustomFieldValue(cf, options)

log.debug "Add to Regression Set value is ${options}"
log.debug "Add to Regression Set checkbox was checked"
break
default:
log.debug "No need to update the Add to Regression Set checkbox"
break
}

Suggest an answer

Log in or Sign up to answer