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

Tony August 16, 2019

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

1 vote
Ravi Sagar _Sparxsys_
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.
August 16, 2019

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

Tony August 16, 2019

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

Ravi Sagar _Sparxsys_
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.
August 16, 2019

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.

Tony August 16, 2019

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)

kresno fatih imani April 11, 2023

using/removing variable 'issue' doesnt work. any updates on this?

Nic Brough -Adaptavist-
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 11, 2023

Welcome to the Atlassian Community!

I'm afraid it does work, there's nothing to update.  You are putting your code in the wrong place (see the conversation about listeners), or not using the issue object correctly in your code.

kresno fatih imani April 11, 2023

here's my code. im not sure what else to do. this is the code from the found here https://community.atlassian.com/t5/Marketplace-Apps-Integrations/Need-help-to-auto-update-Organization-field-in-Jira-SD-when/qaq-p/1099239 

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.servicedesk.api.ServiceDesk
import com.atlassian.servicedesk.api.ServiceDeskManager
import com.atlassian.servicedesk.api.organization.CustomerOrganization
import com.atlassian.servicedesk.api.organization.OrganizationService
import com.atlassian.servicedesk.api.organization.OrganizationsQuery
import com.atlassian.servicedesk.api.util.paging.LimitedPagedRequest
import com.atlassian.servicedesk.api.util.paging.LimitedPagedRequestImpl
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin

@WithPlugin("com.atlassian.servicedesk")

@PluginModule
ServiceDeskManager serviceDeskManager

@PluginModule
OrganizationService organizationService

MutableIssue issue = issue

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
ServiceDesk serviceDeskProject

try {
serviceDeskProject = serviceDeskManager.getServiceDeskForProject(issue.projectObject)
} catch (ignored) {
// if the project is not a Service Desk one then do nothing
return
}

def serviceDeskId = serviceDeskProject?.id as Integer

// get the available organizations for that project
def organizationsQuery = organizationService.newOrganizationsQueryBuilder().serviceDeskId(serviceDeskId).build()

// get all the organizations configured for that project
List<CustomerOrganization> organizationsToAdd

try {
organizationsToAdd = organizationService.getOrganizations(currentUser, organizationsQuery)?.results
} catch(ignored) {
// no organizations found or no access
return
}

// get the Organizations custom field
def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName("Organizations").first()

// finally update the organizations custom field
cf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf), organizationsToAdd), new DefaultIssueChangeHolder())

 

here's the logs

2023-04-11T21:42:20,022 ERROR [workflow.AbstractScriptWorkflowFunction]: Workflow script has failed for user 'admin'. View here: https://myjiraserverdomain.com/secure/admin/workflows/ViewWorkflowTransition.jspa?workflowMode=live&workflowName=T123-P123+-+Bugs+-+Workflow&descriptorTab=postfunctions&workflowTransition=1&highlight=1
com.google.common.util.concurrent.UncheckedExecutionException: java.lang.NullPointerException: null value in entry: issue=null
at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2055) ~[guava-31.0.1-jre.jar:?]
at com.google.common.cache.LocalCache.get(LocalCache.java:3966) ~[guava-31.0.1-jre.jar:?]

please help 

Nic Brough -Adaptavist-
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 11, 2023

That doesn't tell us where you are running this code, or give us the line of script triggering the error.

0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 16, 2019

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.

Tony August 16, 2019

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

Tony August 16, 2019

When I comment out the line

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

ScriptRunner complains that issue is undefined in the subsequent calls.

Ravi Sagar _Sparxsys_
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.
August 16, 2019

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

Tony August 16, 2019

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)
}

Ravi Sagar _Sparxsys_
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.
August 16, 2019

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

Tony August 16, 2019

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)

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 16, 2019

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.

Serena December 6, 2019

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

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 8, 2019

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
Serena December 9, 2019

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
Νίκος Γρέτος July 6, 2020

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