Forums

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

Script Runner - How to Override SIngle Select Field options from Restend Point

Ash August 28, 2021

Hi,

I created a Rest end point which brings data from external API.

I need to set the values from rest end point as options to existing single select field by overriding the default options. Below is Behaviour Script

def testList = getFieldByName("Type List")

def restOptions = get("/rest/scriptrunner/latest/custom/fetchValues")
.asObject(List)
.body.findAll{(it as Map).Items} as List<Map>
testList.setFieldOptions(restOptions)

 

RestEnd Point defined under script runner returns following:

{"items":[{"value":"abcd","html":"abcd","label":"abcd"},{"value":"xyz","html":"xyz","label":"xyz"}],"total":0,"footer":"Choose Types...)"}

 I'm not sure what is wrong doing here and how to call rest end point and get the data and set as options.

 

2 answers

1 accepted

2 votes
Answer accepted
Nic Brough -Adaptavist-
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.
August 29, 2021

It sounds like you have created a select list field to hold the data being read from another system.

This won't work - select lists draw their option list from their config in Jira, you can't change the option list with a behaviour

Ash August 29, 2021

Hi Nic,

Is there any way to call script runner end point from behaviour script and parse the json manually? if so then I think it can be doable. Please advise

Nic Brough -Adaptavist-
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.
August 29, 2021

Again, you can't change the list of options in a select-list field with a behaviour.   You can only add or remove existing options.

Ash August 29, 2021

Ok. Thanks Nick. have to go with textBox field option and will convert into to single select. actually the existing field is used in many project to thought to manage that way.

0 votes
Mohamed Adel
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.
August 29, 2021

Hi @Ash 

welcome to the community , actually i never see if it is possible to use direct get("") method inside the behavior

I used  select list conversion before like this examples 

 

getFieldByName("TextFieldA").convertToSingleSelect([
ajaxOptions: [
url : getBaseUrl() + "/rest/scriptrunner-jira/latest/issue/picker",
query: true, // keep going back to the sever for each keystroke

// this information is passed to the server with each keystroke
data: [
currentJql : "project = SSPA ORDER BY key ASC",
label : "Pick high priority issue in Support project",
showSubTasks: false,

// specify maximum number of issues to display, defaults to 10
// max : 5,
],
formatResponse: "issue"
],
css: "max-width: 500px; width: 500px",
])

 

 

but i see you have a static list in your rest end point where i recommend to set the value directly using Setting Field Defaults 

 

Thanks in advance

Mohamed Adel

 

Ash August 29, 2021

Hi Adel,

 

Thanks a lot for sharing the details.

I already had checked the examples given. those are converting exiting text box field into single select. however, if existing field itself is single select field already and configured with some default options.

What approach I'm guessing is to remove the default options first and then call resend point and convert into options and add it as options..

 

I have written below script as Behaviour initialiser --:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.atlassian.jira.issue.customfields.manager.OptionsManager

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours

def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def testList = getFieldByName("Type List")
def customField = customFieldManager.getCustomFieldObject(testList.getFieldId())
def config = customField.getRelevantConfig(getIssueContext())
optionsManager.removeCustomFieldConfigOptions(config)
def options = optionsManager.createOptions(config,1001,1, [ "Outreach", "Campaign Tracker","html","B772", "A388"])

testList.setFieldOptions(options)

This above code replaced the default options with this new option list and that worked. however, i want this new list from rest end point which returns data in below format.

{"items":[{"value":"abcd","html":"abcd","label":"abcd"},{"value":"xyz","html":"xyz","label":"xyz"}],"total":0,"footer":"Choose Types...)"}

 

So here i have 2 queries:

1. How to call the rest end point to get the data here.

2. convert into value list to create as options.

Mohamed Adel
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.
August 29, 2021

I never see it happened before with a field rather than Text field , you can use the example above and it should work ,for setting field you can only change default values not filling new values.

Suggest an answer

Log in or Sign up to answer