How to get the project key in a ScriptRunner Behaviour

Amir Katz (Outseer)
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.
December 3, 2019

I need to retrieve the current issue's project in a Behaviour groovy script.

I tried using this:

   def myContext = getIssueContext()

but the ScriptRunner interpreter complains that it does not find the method.

I added an import of com.atlassian.jira.issue.context.IssueContext but I still get the same error.

Any idea?

2 answers

1 accepted

1 vote
Answer accepted
Amir Katz (Outseer)
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.
December 19, 2019

I later found out that Will C's answer above does work, but only in some scenarios. In others, the underlyinIssue value is null.

So here is what works for me now:

// Try two ways to get project key in a Behaviour script

String projectKey = null

def projectObj = underlyingIssue?.getProjectObject()

if (projectObj) {  
projectKey = projectObj.key    
else {  
  projectObj = getIssueContext().getProjectObject()  
projectKey = projectObj?.getKey()
  
}

Will C
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.
December 19, 2019

Hi there,

Thanks for replying, can you give me some examples of when underlyingissue is null please?

Just so I dont miss this.

Amir Katz (Outseer)
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.
December 19, 2019

I cannot really tell - I use UserMessageUtil.info() to display debug messages and those popups are displayed only when I refresh the browser tab, so it's hard to tell when exactly they were triggered.

I don't have access to the Jira server so I cannot do a 'tail -f' on the Jira log file(s). When my script was failing, I realized that the projectObj was null, so I added the CYA (:-)) code above.

Maybe, it's null when you create a new issue and non-null when you edit an existing one.

If you have better way to debug Behaviour scripts, please do share.

Thanks again for the original response.

And BTW, in other ScriptRunner scripts that I have (WF validators and post-functions, and script listeners), the issue is always available (event.issue or issue). Only in Behaviour it's problematic.

Will C
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.
December 19, 2019

yea I can understand that the underlyingissue is null before an issue is created.

nope not found a better way to debug behaviours as of yet unfortunately.

4 votes
Will C
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.
December 3, 2019
def proj = underlyingIssue?.getProjectObject()

This should do the trick 

Amir Katz (Outseer)
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.
December 3, 2019

and it does, thanks

Like Will C likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events