Forums

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

ScriptRunner Custom Picker with static options (instead of REST, DB, etc.)

Ben February 14, 2023

Hello, Jira's built-in single select picker does not support both value and label properties so I am attempting to use ScriptRunner's Custom Picker (https://docs.adaptavist.com/sr4js/latest/features/script-fields/built-in-script-fields/custom-picker) to create a very simple select that does this, however I am having some trouble because all examples use either an API or a DB connection to populate the list of options.

Perhaps this is not even the right way to approach this issue, suggestions?

I am not very familiar with Groovy but here is pseudo-code of what I am trying to accomplish:

 

import com.onresolve.scriptrunner.canned.jira.fields.model.PickerOption

def options = [

    {class: 'Foo', description: 'Bar'},

    {class: 'Baz', description: 'Qux'},

    {class: 'Quux', description: 'Corge'},

]

toOption = { Map<String, String> map, Closure<String> highlight ->

    new PickerOption(

        value: map.class,

        label: map.description,

 

        html: highlight(map.description, false),

    )

}

1 answer

1 accepted

3 votes
Answer accepted
PD 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.
February 14, 2023

 I don't have much experience with the custom pickers, but looking the the offered "Version picker" snippets I was able to construct a working sample:

 

import com.onresolve.scriptrunner.canned.jira.fields.model.PickerOption
import org.apache.commons.lang3.StringUtils

def options = [
[id: 'Foo', description: 'Bar'],
[id: 'Baz', description: 'Qux'],
[id: 'Quux', description: 'Corge']
]

search = { String inputValue ->
options.findAll {
StringUtils.containsIgnoreCase(it.description, inputValue)
}
}

getItemFromId = { String id ->
options.find { it.id == id }
}

toOption = { Map<String, String> option, Closure highlight ->
new PickerOption(value: option.id,
label: option.description,
html: "${highlight(option.description, false)} (${option.id})"
)
}

renderItemViewHtml = { Map<String, String> option ->
"$option.description ($option.id)"
}

renderItemTextOnlyValue = { Map<String, String> option ->
option.description
}

The main change from your pseudo code is the correct square braces for initializing a map object and avoiding using "class" as key for a map. Since class in groovy can be confused as a shortcut for the getClass() method.

With this example, you can define whatever as your list of options in the options (functionally a List<Map<String, String>> but def work just fine).

You can change the interpolated stings in renderItemViewHtml and pickerOption.html to combine the id/description differently (including hiding the id completely if that's your preference). Remember tho that the "id" value is what will be stored in the DB. But the description will be stored in the Index and fetched in real-time when you access the issue.

Ben March 1, 2023

Thank you so much, this is working perfectly.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events