Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Setting Checkbox Field Values on Issues.Create

Andrew Wolpers July 27, 2023

Overview

I'm attempting to set the value of a Checkbox field when automagically creating new issues. The value is copied from the issue that triggered the listener in certain scenarios. 

Problem

I'm able to copy the value when I hard code the strings, like:

Issues.create('DEMO', issueType){
setSummary("Test")
setCustomFieldValue("Checkbox Field", "Foo", "Bar")
}
However, if I attempt to grab the values from the trigger issue and reformat to a similar looking stringusing:
def originalCheckboxValue = issue.getCustomFieldValue("Checkbox Field").collect{'"' + it + '"'} as String
def cleanStringValues = originalCheckboxValue.collect{'"' + it + '"'} as String
def grossCleaner = originalCheckboxValue.replaceAll("[\\[\\](){}]","")
 return grossCleaner
The values return as: `"Foo", "Bar" as expected.
and attempt to set like:
setCustomFieldValue("Checkbox Field", grossCleaner) 
Setting the field fails and I receive the error:
com.adaptavist.hapi.jira.issues.exceptions.InvalidOptionException: Invalid option. Available options are: Foo[16500], Bar[16501], Demo[16502], Value[16503]

Question

Has anyone found a way to set checkbox field types when copying the value of an old field? I see a lot for auto-setting the formValue, but not a ton using Issues.create.

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
1 vote
Answer accepted
Peter-Dave Sheehan
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.
July 27, 2023

Generally, in Groovy (maybe in Java too) if you need to supply an argument that is defined as "Class... objects" (they are called variable arguments) and your values are in a list or collection, you can expand your list with "as Class[]"

For example

def myFunction(String... args){
//so something with args
}

def listOfStrings = ['a','b','c']

//call myfunction with listOfStrings
myFunction(listOfStrings as String[])

The HAPI "setCustomFieldValue" method has several signatures that you can leverage. 

The simplest one is if you have the String values for each of your options

def hardCodedListOfCheckBoxValues = ['Foo','Bar']
Issues.create('DEMO', issueType){
setSummary("Test 1")
    setCustomFieldValue("Checkbox Field", hardCodedListOfCheckBoxValues as String[])
}

Another approach if you are reading the option (as List<LazyLoadedOption>) from another issue:

import com.atlassian.jira.issue.customfields.option.Option //Option is the parent interface for LazyLoadedOption
def
originalCheckboxValues = issue.getCustomFieldValue("Checkbox Field") //list of LazyLoadedOptions

Issues
.create('DEMO', issueType){
setSummary("Test 2")
setCustomFieldValue("Checkbox Field", originalCheckboxValues as Option[])
}

Another option is to process each item in your Options list in a separate closure:

def originalCheckboxValues = issue.getCustomFieldValue("Checkbox Field") //list of LazyLoadedOptions
def someListOfOptionsToCopy = ['foo','bar']

Issues
.create('DEMO', issueType){
setSummary("Test 2")
setCustomFieldValue("Checkbox Field"){
//here we can itterate over the originalCheckboxValues and perhaps conditionally add them to the issue
originalCheckboxValues.each{originalOption->
if(originalOption in someListOfOptionsToCopy){
add(originalOption) //the 'add' method here is part of HAPI
}
}
}
}

 

Andrew Wolpers July 28, 2023

Extremely helpful, as always. Thanks for the direction, the LazyLoadedOptions example was exactly what I was looking for.

TAGS
AUG Leaders

Atlassian Community Events