Set field value with rest endpoint

Maxim Kashaev April 20, 2022

I saw information about converting field and connect it to rest endpoint, but can I setFormValue in behaviors via rest endpoint? It returns only one value like: 

{"items":[{"value":3437754,"html":3437754,"label":3437754}]}

Script in behaviors:

getFieldByName("Price").setFormValue([
ajaxOptions: [
url: getBaseUrl() + "/rest/url",
formatResponse: "general",
]
])
And on create screen it set [Object object]

1 answer

0 votes
Peter-Dave 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 22, 2022

I've encountered this.

I normally use formField.convertToSingelSelect([ajaxOptions:[...]])

But I found that I can't also set a value programmatically with that.

So, when i need to set the value dynamically, I use formField.convertToSingelSelect().setFieldOptions(map) instead.

Something like this:

import com.atlassian.jira.component.ComponentAccessor
import groovy.json.JsonParserType
import groovy.json.JsonSlurper
import com.atlassian.sal.api.net.Request
import com.atlassian.sal.api.net.TrustedRequest
import com.atlassian.sal.api.net.TrustedRequestFactory
import groovyx.net.http.URIBuilder
import com.onresolve.scriptrunner.runner.customisers.PluginModule

def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
@PluginModule
TrustedRequestFactory trustedRequestFactory
def options = [:]
options.put("":"") //this is to have a blank option at the top

def url = "$baseUrl/rest/url"
//create a trusted request to the target url
def
request = trustedRequestFactory.createTrustedRequest(Request.MethodType.GET, url) as TrustedRequest
request.addTrustedTokenAuthentication(new URIBuilder(baseUrl).host, currentUser.username)

//prepare the parser
def
parser = new JsonSlurper().setType(JsonParserType.INDEX_OVERLAY)

//execute the request
def
response = request.execute()
def jsonResp = parser.parseText(response)
jsonResp.items.each {
options.put(it.html, it.html)
}
field.convertToSingleSelect().setFieldOptions(options)
field.setFormValue('someDefaultValue')

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events