Forums

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

Scriptrunner conertToSingleSelect Expected Map

Keith Jones
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
May 8, 2020

Hello!

 

I'm trying to create a dynamic field that gets converted to a single select list by the code below.

Does anyone know the exact format of the map this code is expecting?

The list needs to be dynamic because it is generated from a continuously changing list from a large SQL table.

I was able to get it working with a rest endpoint but since the sql request is already elsewhere I would like to generate the map at that time.

Thank you!

 

Here is the code I'm trying to use.

 

def bPartNum = getFieldById("");
def map = [:]

def result = []
for (int i = 0; i < aItemNo.size() ;i++)
{
result << [value:aItemNo[i], html:aItemNo[i],label:aItemNo[i]]
}
//Response.ok(JsonOutput.toJson(map)).build()
map.put('items', result)
bPartNum.convertToSingleSelect(map)

1 answer

1 accepted

0 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.
May 24, 2020

There are 2 ways to perform the conversion

1) have a rest endpoint that returns a structure like:

[ items: [ [value: valueString, html: valueHtml, label: labelString, icon: iconHref] ],
total: numberofItems,
footer: "Prompt when empty

]

 The convertToSingleSelect needs to include ajaxOption parameter like specified in the documentation page.

 

2) use a simple map object. 

//using your aItemNo assuming it is an array of items you want in the map
def map = aItemNo.inject(['':'None']){ map, item ->
map[item]= item
map
}
field.convertToSingleSelect().setFieldOptions( map)

The map will end up looking like this:

[ '': 'None', item1: "item1", item2: "item2", item3: "item3"]

If aItemNo includes key/id and label, you could form the map to look like this:

[ '': 'None', item1key: "Label for item1", item2key: "label for item2", item3Key: "labe for litem3"]

Keith Jones
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
May 26, 2020

Thank you! I have it working now!

 

java.util.Map map = values.inject(['':'None']){ map, item ->
map[item]= item
map
}
def bPartNum = getFieldById("");
bPartNum.convertToSingleSelect().setFieldOptions(map)

 

It didn't work at first with def map so I had to define the map type as java.util.Map

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.
May 26, 2020

Glad it helped. I rarely bother with static typing. The "static type check" errors can often be ignored.

Please remember to accept the answer if it helped you. "Solved" questions are easier for others to find.

Suggest an answer

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

Atlassian Community Events