Hello,
I am trying to code a select list picker scripted field using scriptrunner (jira dc v9.12.2 and scriptrunner v 8.19.0).
My tests are working fine but it looks like the values proposed in the list are not kept up to date with the csv file located on my jira server
Here is my code:
import com.onresolve.scriptrunner.canned.jira.fields.model.PickerOption
import groovy.json.*
String filePath = '/pathtomyfile/monfichiercsv.csv'
//Reading the csv file as an inputstream
List csvMapList = []
new File(filePath).withInputStream { file ->
log.info(file)
file.eachLine { line ->
def tmpMap = [:]
tmpMap.putAt("value", line.toString().toLowerCase())
csvMapList.add(tmpMap)
}
}
search = { String inputValue ->
csvMapList.findAll {it ->
it.'label'.toLowerCase().contains(inputValue.toLowerCase())
}
}
toOption = { Map<String, String> map, Closure<String> highlight ->
new PickerOption(
value: map.value.toString(),
label: map.label.toString(),
html: highlight(map.label, false),
)
}
getItemFromId = { String id ->
def selectedOption = csvMapList.find {it.value.toString() == id }
if (selectedOption) {
selectedOption
} else {
null
}
}
renderItemTextOnlyValue = { Map item ->
item.'label'.toString()
}
I couldn't get your example to work.
But I think what's likely happening is that the scriptrunner/groovy framework is compiling the script into some java class and the csvMapList is instantiated just once when the class is compiled or your application starts.
That's why when you change the script, the file is reloaded (the class is re-compiled).
If you move the loading into a method or closure, you should be able to have this happen dynamically each time the script is called.
Here is your example modified to work in my environment:
import com.onresolve.scriptrunner.canned.jira.fields.model.PickerOption
import groovy.json.*
//Reading the csv file. You don't need an inputstream. Groovy add the eachLine to the file object
List<String> getTermsFromFile() {
List<String> termList = []
String filePath = '/path/to/file.csv'
new File(filePath).eachLine { line ->
termList << line
}
termList
}
search = { String inputValue ->
getTermsFromFile().findAll { it ->
it.toLowerCase().contains(inputValue?.toLowerCase())
}
}
toOption = { String term, Closure<String> highlight ->
new PickerOption(
value: term.toLowerCase(), //here, I'm using the lowercase of the term as the id. That's what will get stored in the DB
label: term,
html: highlight(term, false),
)
}
getItemFromId = { String id ->
def selectedTerm = getTermsFromFile().find { it.toLowerCase() == id }
if (selectedTerm ) {
selectedTerm
} else {
null
}
}
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.