Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.
×Hi all,
I'm working with a Scriptrunner REST Endpoint script that takes record number values from a custom field in jira called Related CMRs, finds the matching records externally from the API of our ServiceNow instance, and displays the CMR query results as json data in a custom multi-text field called CMRs.
Here is the REST Endpoint code as follows:
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
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
import groovy.xml.MarkupBuilder
@BaseScript CustomEndpointDelegate delegate2
CMRDisplay(httpMethod: "GET") { MultivaluedMap queryParams ->
def query = queryParams.getFirst("query") as String
def rt = [:]
if (query) {
String url = "https://test.service-now.com"
String uriPath = "/api/now/table/u_jira_change_data"
HTTPBuilder http = new HTTPBuilder(url)
def output = http.request(Method.GET, ContentType.JSON) {
uri.path = uriPath
uri.query = [sysparm_query:"u_jira_ticket_number=$query", sysparm_fields:"u_change_record.number,u_change_record.short_description,u_change_record.state", sysparm_display_value: "true"]
headers.'Authorization' = "Basic ${"svc-jira:buO\$qguQUgat5lNVF7GH\$3VMtjaR1o".bytes.encodeBase64().toString()}"
response.failure = { resp, reader ->
log.warn("Failed to query ServiceNow API: " + reader.text)
}
}
def cmrState = output["result"]*."u_change_record.state"
def cmrNumber = output["result"]*."u_change_record.number"
def cmrDesc = output["result"]*."u_change_record.short_description"
rt = output
return Response.ok( new JsonBuilder(rt).toString()).build();
}
}
And for a test ticket in our sandbox instance, the resulting data that pulls into the CMRs field is:
{"result":[{"u_change_record.number":"CMR1xxxxxxx","u_change_record.state":"Draft","u_change_record.short_description":"test app req 5"},
{"u_change_record.number":"CMR2xxxxxxx","u_change_record.state":"Draft","u_change_record.short_description":"test"},
{"u_change_record.number":"CMR3xxxxxxx","u_change_record.state":"Draft","u_change_record.short_description":"Test Jira"},
{"u_change_record.number":"CMR4xxxxxxx","u_change_record.state":"Draft","u_change_record.short_description":"tesst"}]}
The data output is working as expected, but I want to display the above resulting json data in a way that's user friendly. I was wondering if I could display the above data as an html table within that multi-text CMR field to look something like this:
Would I take the values pulkled directly from the api (cmrState, cmrNumber, cmrDesc) write html and assign that to the return (rt) value? Is this possible at all?
Many thanks for any tips/suggestions!
Referencing https://scriptrunner.adaptavist.com/latest/jira/fragments/WebItem.html#_dialogs_advanced , I was able to return HTML from the endpoint code the following modified version:
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import groovy.xml.MarkupBuilder
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
import javax.ws.rs.core.MediaType
@BaseScript CustomEndpointDelegate delegate2
CMRDisplay(httpMethod: "GET") { MultivaluedMap queryParams ->
def query = queryParams.getFirst("query") as String
def rt = [:]
if (query) {
String url = "https://test.service-now.com"
String uriPath = "/api/now/table/u_jira_change_data"
HTTPBuilder http = new HTTPBuilder(url)
def output = http.request(Method.GET, ContentType.JSON) {
uri.path = uriPath
uri.query = [sysparm_query:"u_jira_ticket_number=$query", sysparm_fields:"u_change_record.number,u_change_record.short_description,u_change_record.state", sysparm_display_value: "true"]
headers.'Authorization' = "Basic ${"svc-jira:buO\$qguQUgat5lNVF7GH\$3VMtjaR1o".bytes.encodeBase64().toString()}"
response.failure = { resp, reader ->
log.warn("Failed to query ServiceNow API: " + reader.text)
}
}
def cmrState = output["result"]*."u_change_record.state"
def cmrNumber = output["result"]*."u_change_record.number"
def cmrDesc = output["result"]*."u_change_record.short_description"
def table =
"""<table class="aui aui-table-list" aria-hidden="true">
<thead>
<tr>
<th id="record-num">Record Number</th>
<th id="record-desc">Description</th>
<th id="record-state">State</th>
</tr>
</thead>
<tbody>
<tr class="aui-row-subtle">
<td headers="record-num">${cmrNumber}</td>
<td headers="record-desc">${cmrDesc}</td>
<td headers="record-state">${cmrState}</td>
</tr>
</tbody>
</table>
"""
Response.ok().type(MediaType.TEXT_HTML).entity(table.toString()).build()
}
}
To the end user, the data now appears as:
My only problem now is that the rows don't generate as expected. Obviously, there should be 4 rows in total, with [CMR1..., test app req 5, Draft] in one row, then [CMR2..., test, Draft] as the second row, and so on. But instead, the output results are being crammed into one row.
Additionally, the css class attributes don't take effect for some reason. Referencing Jira's AUI Documentation, I figured that the CSS would automatically apply when referencing the 'aui' class in for the table.
I'll post another question in the community to hopefully find the solution to that, but as far as displaying JSON Data as HTML, I figured that part out.
Atlassian Government Cloud has achieved FedRAMP Authorization at the Moderate level! Join our webinar to learn how you can accelerate mission success and move work forward faster in cloud, all while ensuring your critical data is secure.
Register Now
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.