Forums

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

How to add options to select list using rest call

G Krithica
Contributor
April 19, 2019

Hi,

Using the rest end point and behaviour scriptrunner i was able to convert a text field into dropdown list which fetches the values using rest call. As in the link: https://scriptrunner.adaptavist.com/4.3.6/jira/behaviours-conversions.html#_walkthrough_external_rest_service

Script used:

getFieldByName("textfield").convertToSingleSelect([
ajaxOptions: [
url : getBaseUrl() + "/rest/scriptrunner/latest/custom/query1",
query: true,
formatResponse: "general"
]
])

This works perfectly.

I want the same functionality with single select list field.

i am trying with below script,

getFieldByName("selectlist").setFieldOptions([
ajaxOptions: [
url : getBaseUrl() + "/rest/scriptrunner/latest/custom/query1",
query: true,
formatResponse: "general"
]
]) .

The field displays [Object Object].

How to display the options in the select list field?

2 answers

0 votes
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.
April 19, 2019

The select list field has no values configured in jira. The list has to take the value only from the rest call. When using setFieldOptions the list field only shows 

This is not possible. Single Select field and Multiselect field store a relational "option" id in the database. So that option must exist for a given issue context (project and issue type).

setFieldOptions will only let you control which of the existing options should appear in the select field.

If you want values from a rest call, you can only do that with a text field. 

Our approach was to create a new text field to replace the old select, write a fairly complex and custom script to update all issues with a select value into the text field then retire the select field.

Report and dashboard that were based on the old select fields had to be identified and fixed to use the new text field (with single select conversion).

G Krithica
Contributor
April 21, 2019

Thank you 

Rodolfo So October 30, 2021

@G Krithica 

Can you share your complete script for text field using rest api? I’m planning to use this ServiceNow integration.

 

Appreciate your help on this

 

Thanks

0 votes
Tarun Sapra
Community Champion
April 19, 2019

Hello @G Krithica 

Using behaviour module it's possible to use setFieldOptions to set the value for a select list type of field

https://scriptrunner.adaptavist.com/latest/jira/behaviours-api-quickref.html#_formfield_setfieldoptions_iterable

But using this you can't modify the existing values of the custom field present on the server. To do that you will need to use script runner plugin java server side code exposed as a REST endpoint.

https://docs.atlassian.com/software/jira/docs/api/7.6.1/com/atlassian/jira/issue/customfields/manager/OptionsManager.html

G Krithica
Contributor
April 19, 2019

The select list field has no values configured in jira. The list has to take the value only from the rest call. When using setFieldOptions the list field only shows [Object Object].

Tarun Sapra
Community Champion
April 19, 2019

Hello @G Krithica 

Could you elaborate on

The list has to take the value only from the rest call

Because the method setFieldOptions is for setting the values based on already existing values of the custom field but if there are no values of the custom field then setFieldOptions will always no values to set. Hence the custom field on the server has to have values in order for setFieldOptions  to set values.

see example here

https://scriptrunner.adaptavist.com/latest/jira/behaviours-api-quickref.html#_formfield_setfieldoptions_iterable

it's setting values based on existing values of the custom fields.

Tarun Sapra
Community Champion
April 21, 2019

Hello @G Krithica 

If the answer helped you, please accept/upvote the answer so that others are helped as well.

Suggest an answer

Log in or Sign up to answer