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.
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")
}
def originalCheckboxValue = issue.getCustomFieldValue("Checkbox Field").collect{'"' + it + '"'} as String
def cleanStringValues = originalCheckboxValue.collect{'"' + it + '"'} as String
def grossCleaner = originalCheckboxValue.replaceAll("[\\[\\](){}]","")
return grossCleaner
setCustomFieldValue("Checkbox Field", grossCleaner)
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
}
}
}
}
Extremely helpful, as always. Thanks for the direction, the LazyLoadedOptions example was exactly what I was looking for.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.