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.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
using/removing variable 'issue' doesnt work. any updates on this?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
That doesn't tell us where you are running this code, or give us the line of script triggering the error.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
When I comment out the line
def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("GHD-1234")
ScriptRunner complains that issue is undefined in the subsequent calls.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Can you paste the whole code and you are writing it as "Custom script post-function"?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I didn't check the whole code but looks like it should work. Try ignoring the error and give it a try.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
A post-function is not a listener. Post-functions get the "issue" object, but listeners are listening for "events"
Try event.issue() instead.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.