Forums

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

Set options for a multi select list from REST API endpoint

James Paniagua
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!
February 4, 2020

Greetings,

My first post here: I have some external data (in a JSON format from a REST API endpoint) that I need to input into a multi select list (custom field) that is in Jira - the reason being I'd to do this via a script (ScriptRunner/Groovy or Java) is that the data changes frequently and there is a large volume of options, which makes manual maintenance of the custom field options very tedious and wasteful of time. I wanted to inquire if anyone else has had this challenge and how you've tackled it. There are two possible ways I can think of...

1) Write a "sync" script which runs on a regular basis that parses the endpoint JSON response and adds only new, unique options into the custom field via REST.

2) Although I haven't verified, would be to set options via a behavior (ScriptRunner) for the custom field, I've searched around and found code similar to below that has curated only specific options based on the definition found below - has anyone fed a REST API response into the options (rather than literally specifying each option below)? 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.manager.OptionsManager
def optionsManager = ComponentAccessor.getComponent(OptionsManager)

def admin_field = getFieldByName("Administrator Access?")
admin_field.setAllowInlineEdit(false)

def admincustomField = customFieldManager.getCustomFieldObject(admin_field.getFieldId())
def adminconfig = admincustomField.getRelevantConfig(getIssueContext())
def adminOptionsOriginal = optionsManager.getOptions(adminconfig)

//define preferred options here
def admin_field_curated_options = adminOptionsOriginal.findAll {it.value in ['Yes', 'No']}

admin_field.setFieldOptions(admin_field_curated_options)

 

1 answer

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.
February 4, 2020

Have you explored the Select List Conversion feature?

With this, you change your custom field to a free text field. But set the searcher/indexer to "Exact Text Searcher" so that it behaves like a select in JQL and gadgets etc.

Then use the conversion method in behavior to change how the field is presented to the user.

I like this because you can have your user type in the dropdown and only send the request to your API based on the user's input and only retrieve matching records.

The behavior script option you listed requires the options to exist in the field's configuration. You can only filter the options, not add to them.

If you really need to keep the full select fields features, you could, in theory, have your behaviour script make the rest call to get the list of remote options then compare against the existing options then make adjustments to the relevant configurations for the custom field based on that comparison (add, remove or disable options). But this comparison will have to be done every time a user opens a screen that includes that field. It might put unnecessary performance pressure on your environment.

So, that brings it back to your first idea... periodically update the options using a service that runs on a schedule.

In either case, if you want to programmatically manage the options in a custom field context, you will need to use the OptionsManager with something along those lines (untested):

import com.atlassian.jira.component.ComponentAccessor
optionsManager = ComponentAccessor.optionsManager
customFieldManager = ComponentAccessor.customFieldManager

def formField = getFieldByName("fieldname")

def customField = customFieldManager.getCustomFieldObject( formField.getFieldId() )
def fieldConfig= customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(fieldConfig)

def newOptionValue = "What value to add"
def newOption = optionsManager.createOption(fieldConfig, null, options.size()+1 , newOptionValue)

options << newOption

optionsManager.updateOptions(options) 

Suggest an answer

Log in or Sign up to answer