How can I use Groovy Script to get an issue id in a post function? issue.getId() returns null

Patrick S
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 6, 2017

I'm sure I'm overlooking something simple, as getId() seems to be exactly what I should need.  I've tried issue.getID(), getKey(), etc, to no avail.

 

import groovy.sql.Sql
import java.sql.Driver
import com.atlassian.jira.issue.Issue import com.atlassian.jira.issue.ModifiedValue import com.atlassian.jira.issue.util.DefaultIssueChangeHolder import com.atlassian.jira.component.ComponentAccessor
/* I've tried with and without this declaration as well
Issue issue = issue
*/
// All of these return null. I need to retrieve the id for use in a SQL query later in the script. log.error("id: " + issue.getId()) // returns null log.error("key: " + issue.getKey()) // returns null log.error("number: " + issue.getNumber()) // returns null log.error("issue: " + issue) // returns the Summary field of the issue

Log results

2017-04-06 15:07:37,332 ERROR [workflow.ScriptWorkflowFunction]: id: null
2017-04-06 15:07:37,332 ERROR [workflow.ScriptWorkflowFunction]: key: null
2017-04-06 15:07:37,332 ERROR [workflow.ScriptWorkflowFunction]: number: null
2017-04-06 15:07:37,332 ERROR [workflow.ScriptWorkflowFunction]: issue: sdfsadfasd

1 answer

1 accepted

2 votes
Answer accepted
Patrick S
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 6, 2017

I'm an idiot.  The reason it was returning null is because the Custom Script post-function was being called before the "Creates the issue originally" post-function.  Re-ordering the post-function calls resolved this.

Hopefully this saves someone else some time in the future.

smetannikov May 16, 2018

Hi, Patrick!

Please tell me how you did it:

"Re-ordering the post-function calls"

?

Thank!

Patrick S
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 16, 2018

@smetannikov,

In the workflow that called the script, I had to move the "ScriptRunner workflow function" to be after the "Creates issue initially" step.  This makes sense, the ID would be null because the issue doesn't exist yet.

 See screenshot of the Workflow post function order below.

screenshot.png

smetannikov May 16, 2018

Patrick, thanks!

It's really very very simple)

Arjun Hazari September 9, 2020

This was really helpful I finally retrived the project key from this

 

log.error("Project Key:" + issue.getProjectObject().getKey() ) 
Like Patrick S likes this
Benjamin Horst
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.
March 2, 2023

It took me a few seconds before I upvoted an answer that stated "I am an idiot" :)

Like Patrick S likes this
Patrick S
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 3, 2023

I'll start answering more of my own questions with that, and no further explanation, to remove any doubt that it's true.

Like Benjamin Horst likes this

Suggest an answer

Log in or Sign up to answer