Scriptrunner: Form autofill: Use single select value to fill additional fields

Ludwig Mader
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!
May 15, 2019

Hi, 

i am searching for a snipped or guide.

I try to get data from a JSON REST in a single select field in a form.

This is working well.

But i want to fill some more fields based on the chosen JSON result.

 

Example:

JSON Result = SystemName1, Value1, Option1

 

If i chose now "SystemName1" from my single select field, i want to to fill the "Value" and "Option" field filled with Value="Value1" and Option="Option1".

 

This is my current impelementation of the single select query:

 

import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.json.JsonOutput
import groovy.transform.BaseScript
import groovyx.net.http.ContentType
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method

import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response

@BaseScript CustomEndpointDelegate delegate

SLACustomerQuery(httpMethod: "GET") { MultivaluedMap queryParams ->

def query = queryParams.getFirst("query") as String

def rt = [:]
if (query) {
def httpBuilder = new HTTPBuilder("http://xxxxxxxxxxx:xxxxxxxxxx")
def objects = httpBuilder.request(Method.GET, ContentType.JSON) {
uri.path = "/abc"
uri.query = [system_name: "ilike.*$query*", select: "system_name,id"]

response.failure = { resp, reader ->
log.warn("Failed to query SLAObjects API: " + reader.text)
return Response.serverError().build()
}
}

rt = [
items : objects.collect { Map object ->
def objectName = object."system_name"
def objectId = object."id"
[
value: objectId + " " + objectName,
html : objectName.replaceAll(/(?i)$query/) { "<b>${it}</b>" } + " (${objectId})",
label: objectName
]
},
total : objects.size(),
footer: "Choose SLAObject... (${objects.size()} shown...)"
]

}

return Response.ok(new JsonBuilder(rt).toString()).build()
}

0 answers

Suggest an answer

Log in or Sign up to answer