Forums

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

Set value on a field with convertToSingleSelect

Vlad Petrescu Life in Codes
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!
April 8, 2021

I am using Scriptrunner 6.20.0 and I have converted a text Field to a SingleSelect and it displays fine, plus I can select an option in the form. Just what I wanted.

def tribeField = getFieldByName("Tribe")
tribeField.convertToSingleSelect([
ajaxOptions: [
url : getBaseUrl() + "/rest/scriptrunner/latest/custom/...",
query : true,
formatResponse: "general"
]
])

The problem is that I cannot set a value in the field programmatically.

"tribe1" is one of the values returned from the REST endpoint, and I have tried many things, before or after calling convertToSingleSelect, like:

tribeField.setFormValue("tribe1")               
tribeField.setFieldOption(["tribe1": "tribe1"])

The second line sets an option in the hidden input HTML element, which is not valid:

<input class="text full-width-field" type="text" name="customfield_20208" id="customfield_20208" aria-labelledby="customfield_20208-label customfield_20208-helper" style="display: none;">
<option value="tribe1">tribe1</option>
</input>

 

This is exactly my problem, but the answer did not help at all: https://community.atlassian.com/t5/Answers-Developer-Questions/Set-value-on-text-field-converted-to-select-list-with/qaq-p/548053

Any ideas?

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.
April 8, 2021

I've encountered this and raised a support request a few years back.

They pointed me to https://productsupport.adaptavist.com/browse/SRJIRA-2168 but that was ultimately fixed with the issue picker custom field rather than adjusting the convertToSingleSelect() functionality.

I never tried the solution you pointed to... I see in your example that you setFieldOption (no s) and in the linked post, they  have setFieldOptions (with s).

I would recommend you try that first.

If you can't get that to work as is... the workaround is to:

1) Call your API and load all the data into a Map object, then

2) convert your field to a single select without any parameters and set the field options from your map. 

def map =[:]
map.put("","") //include an empty choice so you can clear it
def httpBuilder = new HTTPBuilder(getBaseUrl())
httpBuilder.request(Method.GET, ContentType.TEXT) {
uri.path = "/rest/scriptrunner/latest/custom/..."
uri.addQueryParam 'pkey', project.key
headers."accept" = ""
headers."Authorization" = "Basic xxx" //api user
response.success = { response, reader ->
def jsonText = reader.text
def parser = new JsonSlurper().setType(JsonParserType.INDEX_OVERLAY)
def jsonResp = parser.parseText(jsonText)
jsonResp.items.each{
map.put(it.html, it.html)
}
}
response.failure = {resp, reader ->
log.error("Getting data from ... : ${resp.statusLine}")
}
}

field.setFormValue(map.get('tribe1')
field.convertToSingleSelect().setFieldOptions(map)

Or, move your custom REST API code into the behaviour and load the map directly from java api calls rather than have the server communicate with itself over http

Vlad Petrescu Life in Codes
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!
April 13, 2021

Hi @PD Sheehan ,

Thank you for you answer. I initially tried with setFieldOptions, setFieldOption and other methods that do not exist, with no success.

Going to your workaround, I think it should work, but I will loose the backend autocomplete feature, which I need since the list has +500 items in it and would load slow.

Initially, the field was of type text, and calling field.convertToSingleSelect() with no parameters on it fails, as the JavaScript code is trying to get the ajaxOptions parameter which does not exist. 

minimumInputLength: v.ajaxOptions.minQueryLength || 0,
//This ScriptRunner JavaScript line throws a browser error and the rendering stops.


I switched to a Single Select type field and I can populate it and change values in the field programmatically, but I lost the backend filtering. I wonder if there's a better solution.

Suggest an answer

Log in or Sign up to answer