It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

For a workflow post function, how to get the current issue key without knowing issue id? Edited

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.worklog.WorklogImpl
import com.atlassian.jira.event.type.EventDispatchOption

def issueKey = ?
def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey(issueKey)
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_12345")

.......

I need to get the issueKey of the issue that is calling this workflow post function.  Can't figure out how to do that.  

The code works if I hard-code the issueKey.

2 answers

0 votes

Hi @Tony 

You don't need to declare and fetch issue. It is already available as a binding variable in postfunction. There is a small question mark sign in the script editor that can help you in finding available binding variable.

Also take a look at this example.

Ravi

Thank you.  In the example, what is the "binding variable" to which you referred to in your response?

All the variables that are related to issue, event, user, etc that are applicable for the functionality where you are writing the script.

If you are using the new version of ScriptRunner just type issue followed by dot and then press Ctrl + space key. You will get the list of all the methods as suggestions. Use the one you need in your script. It is quite good for writing code directly in your Jira.

The Ctrl + Space key does not provide any response at all.

I get an error when just typing in issue.

I am using ScriptRunner 5.5.7  (not the latest)

0 votes

During a post function, you have direct access to the issue object that the post function is currently running against.

So while you can get the key with just

issue.getKey()

You don't need to, because you can just directly use the issue already!  You "def issue =" line would be overwriting an object you already have.

So are you saying issueKey already exists (is populated with current issue key)?

When I comment out the line

def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("GHD-1234")

ScriptRunner complains that issue is undefined in the subsequent calls.

Can you paste the whole code and you are writing it as "Custom script post-function"?

Yes, a custom script post-function (with hard-coded values, it works):

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.worklog.WorklogImpl
import com.atlassian.jira.event.type.EventDispatchOption

def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("12345")
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_121212")
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def fieldVal = (Float) issue.getCustomFieldValue(customField)
def hrs = (Float) issue.getCustomFieldValue(customField) * (Float) 3600.00
def worklogManager = ComponentAccessor.getWorklogManager()
def worklog = new WorklogImpl(worklogManager, issue, null, issue.reporter.name, issue.summary, new Date(), null, null, (Long) hrs)

if ( fieldVal > 0.0 ) {
worklogManager.create(issue.reporter,worklog, 0L, false)
issue.setCustomFieldValue(customField,(Double) 0) 
ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)
}

I didn't check the whole code but looks like it should work. Try ignoring the error and give it a try.

The above code does work.  But how to get the current issue key.

So i can replace

def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("12345")

with something dynamic like

def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey(the_current_issue_key)

As I said in my answer, you do not. 

Your "def issue =" line is destroying the issue that you already have because you are in a post-function.

Remember that you do not have access to the issue in the console, which is where I suspect you are testing your script.  Please put the code (without "def issue =" lines) in a post-function, you will find that issue.getKey() then gives you the key of the current issue.

Hello @Tony 

I have your same problem.

Did you discover the solution? i'd like to implement a Listeners script funcion but i can't obtain  the dynamic current issue key.

Thank You

Serena

A post-function is not a listener.  Post-functions get the "issue" object, but listeners are listening for "events"

Try event.issue() instead.

Like Hiệp_Nguyễn likes this

Thank you for your response @Nic Brough _Adaptavist_ . I have resolved the problem using event.issue() in Scriptrunner Listeners Inline Funcion. It works fine. In this way using the code:

def issueKey = event.getIssue().getKey().toString();
def issueSummary = event.getIssue().getSummary().toString();
IssueManager im = ComponentAccessor.getIssueManager();
MutableIssue issue = im.getIssueObject(issueKey);

I got an issue object and I was able to manage it.

Like Hiệp_Nguyễn likes this

I have the following code in a post function on create transition

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.user.util.UserManager 
import com.atlassian.jira.event.type.EventDispatchOption

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def groupManager = ComponentAccessor.getGroupManager()
def issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def multiGroupCf = customFieldManager.getCustomFieldObjectByName("Demo-Π.Σ.Κ.Ε. Assignable Groups") //multigroup picker custom field

def group1 = groupManager.getGroup("EYKE Group") //jira group
def group2 = groupManager.getGroup("Group_Epilogi_Proistamenon") //jira group
def group3 = groupManager.getGroup("ISO - Security Officers") //jira group

def groupList = [group1, group2, group3]

issue.setDescription("lalal")
issue.setCustomFieldValue(multiGroupCf, groupList)

The use of issue keyword does not work and when I create a new issue I'm getting the following message:

"We can't create this issue for you right now, it could be due to unsupported content you've entered into one or more of the issue fields. If this situation persists, contact your administrator as they'll be able to access more specific information in the log file."

 

So, it seems that issue variable cannot be used to get the current issue.

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you