How to get a specific issue object by id in Script Runner for Jira Cloud

Deleted user January 8, 2018

I am attempting to troubleshoot a script runner workflow post function in the script runner console, and would like to assign the default issue object to a specific issue by id so I can use the console to run and edit the failing code.

How can I assign the issue object to a specific issue by ID? I would expect something like:

     def issue = getIssueByID("JRA-123")

so that I can then do stuff like:

     def fieldValue = issue.fields[fieldID] as String

1 answer

1 accepted

2 votes
Answer accepted
Jenna Davis
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.
January 8, 2018

Hello, 

You can use the IssueManager from JIRA's API to do this. I believe specifically that getIssueObject('your Issue ID') will do what you are looking for. 

You'll also probably want to take a look at Issue and MutableIssue for information on how to get and set values in an issue. 

Hope this helps you out!
Jenna

Deleted user January 8, 2018

Hi Jenna,

Thanks for responding. All of that makes sense, I just can't seem to figure out how to make that kind of call using the REST APIs since I'm in the cloud environment and can't use the Java API. 

It seems like maybe it should be as simple as:

     def issue = get('/rest/api/2/issue/JRA-123')

but that throws the following error, so I imagine I need to explicitly map it maybe?

com.fasterxml.jackson.databind.JsonMappingException: Direct self-reference leading to cycle (through reference chain: com.adaptavist.sr.cloud.output.ScriptExecutionOutput["result"]->groovy.util.Proxy["adaptee"]->com.mashape.unirest.request.GetRequest["httpRequest"])
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter._handleSelfReference(BeanPropertyWriter.java:781)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:569)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:666)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:156)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:575)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:666)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:156)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:575)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:666)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeWithType(BeanSerializerBase.java:552)
at com.fasterxml.jackson.databind.ser.impl.TypeWrappedSerializer.serialize(TypeWrappedSerializer.java:32)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:129)
at com.fasterxml.jackson.databind.ObjectWriter._configAndWriteValue(ObjectWriter.java:1052)
at com.fasterxml.jackson.databind.ObjectWriter.writeValueAsString(ObjectWriter.java:923)
at com.fasterxml.jackson.databind.ObjectWriter$writeValueAsString.call(Unknown Source)
at com.fasterxml.jackson.databind.ObjectWriter$writeValueAsString.call(Unknown Source)

Deleted user January 8, 2018

Hi Jenna,

Thanks for responding. All of that makes sense, I just can't seem to figure out how to make that kind of call using the REST APIs since I'm in the cloud environment and can't use the Java API. 

It seems like maybe it should be as simple as:

     def issue = get('/rest/api/2/issue/JRA-123')

but that throws the following error, so I imagine I need to explicitly map it maybe?

com.fasterxml.jackson.databind.JsonMappingException: Direct self-reference leading to cycle (through reference chain: com.adaptavist.sr.cloud.output.ScriptExecutionOutput["result"]->groovy.util.Proxy["adaptee"]->com.mashape.unirest.request.GetRequest["httpRequest"])
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter._handleSelfReference(BeanPropertyWriter.java:781)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:569)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:666)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:156)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:575)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:666)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:156)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:575)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:666)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeWithType(BeanSerializerBase.java:552)
at com.fasterxml.jackson.databind.ser.impl.TypeWrappedSerializer.serialize(TypeWrappedSerializer.java:32)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:129)
at com.fasterxml.jackson.databind.ObjectWriter._configAndWriteValue(ObjectWriter.java:1052)
at com.fasterxml.jackson.databind.ObjectWriter.writeValueAsString(ObjectWriter.java:923)
at com.fasterxml.jackson.databind.ObjectWriter$writeValueAsString.call(Unknown Source)
at com.fasterxml.jackson.databind.ObjectWriter$writeValueAsString.call(Unknown Source)

Deleted user January 8, 2018

Hi Jenna,

Thanks for responding. All of that makes sense, I just can't seem to figure out how to make that kind of call using the REST APIs since I'm in the cloud environment and can't use the Java API.

It seems like maybe it should be as simple as:

def issue = get('/rest/api/2/issue/JRA-123')

but that throws the following error, so I imagine I need to explicitly map it maybe?

com.fasterxml.jackson.databind.JsonMappingException: Direct self-reference leading to cycle (through reference chain: com.adaptavist.sr.cloud.output.ScriptExecutionOutput["result"]->groovy.util.Proxy["adaptee"]->com.mashape.unirest.request.GetRequest["httpRequest"])

Jenna Davis
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.
January 8, 2018

Ahh, sorry. I completely missed that you were using cloud. You should be able to do something close to this:

def issueKey = "TEST-1"
def currentIssue = get('/rest/api/2/issue/' + issueKey)
        .asObject(Map)

 Here is some documentation that might help you out further also. :)

Jenna

Deleted user January 8, 2018

Thanks, .asObject(Map) was the piece I was missing.

Alex Palermo September 26, 2018

@[deleted]  can you pass me the example you used to get the value of a field ?

brian.baxter July 9, 2020

I am also looking for information on how to get subtasks as objects. 

Using this example:

def issueKey = "TEST-1"
def currentIssue = get('/rest/api/2/issue/' + issueKey)
        .asObject(Map)

I would expect that I could use:

def subtasks = currentIssue.fields.subtasks

but that gives me an error: No such property subtasks...

Tamsin October 28, 2020

I had the same problem, until I realised that all the data is inside the body element.

So this should work:

def subtasks = currentIssue.body.fields.subtasks

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events