So, I've been struggling to get the forms via API using Scriptrunner. In general, Atlassian documentation is omitting one crucial part - formid which needs to be pulled first.
So, if anyone needs it, here's a code:
- pulls the form ID from an existing issue
- authenticates to api.atlassian.com
- pulls the form details
import javax.xml.bind.DatatypeConverter
def issueKey = XXX-123
def username = 'NAME'
def password = 'Auth Token'
def authString = "${username}:${password}"
def encodedAuthString = DatatypeConverter.printBase64Binary(authString.getBytes('UTF-8'))
def formId = get("/rest/api/2/issue/${issueKey}/properties/proforma.forms")
.header('Content-Type', 'application/json')
.asObject(Map).body
def id = formId.value.forms[0].uuid
def fields = get("https://api.atlassian.com/jira/forms/cloud/CLOUDID/issue/${issueKey}/form/${id}")
.header('Content-Type', 'application/json')
.header('Authorization', "Basic ${encodedAuthString}")
.header('X-ExperimentalApi', 'opt-in')
.asObject(Map).body
It pulls the first form, but you can iterate, etc. Hope it will make somebody's day easier ;)
thanks a lot for the sharing :-)
I can't get this to work. Any thoughts? my ticket definitely has a form on it
getting this error
java.lang.NullPointerException: Cannot get property 'forms' on null object at Script_d6a87e4086ed1f81632a13d3d3cafb1b.run(Script_d6a87e4086ed1f81632a13d3d3cafb1b.groovy:17) at com.adaptavist.sr.cloud.workflow.AbstractScript.evaluate(AbstractScript.groovy:35) at com.adaptavist.sr.cloud.workflow.AbstractScript.runScriptFromRequest(AbstractScript.groovy:98) at com.adaptavist.sr.cloud.events.ScriptExecution.run(ScriptExecution.groovy:59) at ConsoleScriptExecution1_groovyProxy.run(Unknown Source)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I was able to get as far as this and after testing each call in postman and verifying i get results, my last return (fields4) does not return any data
import javax.xml.bind.DatatypeConverter
import groovy.json.JsonSlurper
// --- CONFIG ---
def issueKey = 'HR-1244'
def username = 'XXXXX'
def password = 'XXXXXX0"
// --- AUTH GEN ---
def authString = "${username}:${password}"
def encodedAuthString = DatatypeConverter.printBase64Binary(authString.getBytes('UTF-8'))
// --- THE REQUEST ---
def response = get("https://api.atlassian.com/jira/forms/cloud/${cloudId}/issue/${issueKey}/form")
.header('Authorization', "Basic ${encodedAuthString}")
.header('Accept', 'application/json') // Added to ensure JSON return
.header('X-ExperimentalApi', 'opt-in') // Required for most Forms API calls
.asString() // We use asString to see the raw output if it's not JSON
//return response.body
def responseBody = response.body
// 1. Parse the JSON string into an object
def slurper = new JsonSlurper()
def slurper1 = new JsonSlurper()
def formsList = slurper.parseText(responseBody)
// 2. Access the first item in the list [0]
// and navigate to formTemplate -> id
def templateId = formsList[0]?.id
return templateId //does return what i expect
// 3. Call the specific form instance to get the answers
def fields4 = get("https://api.atlassian.com/jira/forms/cloud/${cloudId}/issue/${issueKey}/form/${templateId}")
.header('Authorization', "Basic ${encodedAuthString}")
.header('Accept', 'application/json') // Added to ensure JSON return
.header('X-ExperimentalApi', 'opt-in') // Required for most Forms API calls
.asString() // We use asString to see the raw output if it's not JSON
return fields4
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.