Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to iterate through checkbox values with Scriptrunner script?

Maya_Chase May 22, 2018

Hi, thanks in advance for anyone who can help me out, here. I have the following script:

import com.atlassian.jira.issue.Issue
import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Category

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()
def fieldConfigSchemeManager = ComponentAccessor.getFieldConfigSchemeManager()

def cf = customFieldManager.getCustomFieldObjectByName("New Hire Account provisioning")
def config = fieldConfigSchemeManager.getRelevantConfig(issue, cf)
def allOptions = optionsManager.getOptions(config)
def checkboxFieldValues = cf.getValue(issue)

def cField = customFieldManager.getCustomFieldObject('customfield_15564')
def cFieldValue = issue.getCustomFieldValue(cField)

def cField2 = customFieldManager.getCustomFieldObject('customfield_16662')
def cFieldValue2 = issue.getCustomFieldValue(cField2)

log.error(checkboxFieldValues)

checkboxFieldValues.each { optionValue ->
log.error(optionValue)
log.error(optionValue.toString())
if ((optionValue.toString() == "Egencia") && ((cFieldValue == null) || (cFieldValue2 == null)))
throw new InvalidInputException("Manager Email", "Both Department Number and Manager Email are required when specifying an Egencia account be set up.")
}

---------------------

The idea is to loop through the values of a checkbox field, check that a specific value is checked and either/both of two other fields is null, and then throw an exception based on that. However, evidently I'm not pulling out the values of the checkboxes correctly, as the first log message gives me 

2018-05-22 16:08:55,870 ERROR [workflow.ScriptWorkflowFunction]: 

instead of a list of values, and the two log messages inside the each loop are never hit. I feel like I've tried every method on the web and NONE of them work for checkboxes - they either don't even pass a syntax check, or they don't produce any values.

 

1 answer

1 vote
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.
May 23, 2018

Hi Maya, 

Here is an example of how to get the selected values for a checkbox custom field for issue with key JRA-1

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

def issue = ComponentAccessor.issueManager.getIssueByCurrentKey("JRA-1")

def checkboxes = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue).find {it.name == "Checkboxes"}
def checkedValues = (checkboxes?.getValue(issue) as List <LazyLoadedOption>)*.value

assert checkedValues?.contains("Yes") //check if one of the selected values is 'Yes'

Hope that helps.

Regards, Thanos

Suggest an answer

Log in or Sign up to answer