Need help with JIRA groovy script

Hi - I am having some trouble getting a post function script to run in JIRA.  I am very inexperienced in coding and do not know why I keep getting the error that [issue] is undeclared.  This is JIRA v6.

 

The purpose of this post function script is to update Assignee based on Issue Type:

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.config.SubTaskManager
import org.ofbiz.core.entity.GenericValue
import com.atlassian.jira.user.UserUtils
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.user.util.UserManager
import com.opensymphony.workflow.WorkflowContext
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.component.ComponentAccessor

if(issue.issueTypeObject.name == 'Story') {
issue.setAssigneeId('User1')
}
else if(issue.issueTypeObject.name == 'Bug') {
issue.setAssigneeId('User2')
}
else if (issue.issueTypeObject.name == 'Problem') {
issue.setAssigneeId('User3')
}

2 answers

1 accepted

This widget could not be displayed.

This has been resolved.  Although the script console gave me that error, it seemed to execute perfectly when put inside the post-create workflow transition as follows:

import com.atlassian.jira.ComponentManager

import com.atlassian.jira.issue.Issue

if(issue.issueTypeObject.name == 'Bug') {
issue.setAssigneeId('User1')
}
else if(issue.issueTypeObject.name == 'Problem') {
issue.setAssigneeId('User2')
}
else if (issue.issueTypeObject.name == 'Defect') {
issue.setAssigneeId('User3')
}

Did you test the scirpt? 
Did it actually assigned the issue to the inteneded user just like this without validation?

Yes, I did test it and it worked inside the project context in the workflow transition script. 

It assigns to the specific user set in the script, however, this needs to be placed at the correct step in the create issue post functions. 

This widget could not be displayed.
Steven Behnke Community Champion Sep 07, 2017

Can you show us where you're using this script? Also, you don't need any of those IMPORT statements. 

I am using this as a script after creating an issue that would assign to a specific user based on the issue type. 

This would go in the "post condition" for the create transition. 

When I type it in the script console I get a "[issue] variable not declared"

I am stummed at this as well. 
In the following, I am just getting an issue, a user, and assigning the issue to that user. 

Why is this not working?
It just keeps returning null without changing the assignee.

import com.atlassian.jira.component.ComponentAccessor

def issueManager = ComponentAccessor.getIssueManager()
def userManager = ComponentAccessor.getUserManager()

def user = userManager.getUserByName("tbas")

def issueObject = issueManager.getIssueObject("HW_TEST-64")
def issueType = issueObject.getIssueType().name

issueObject.setAssignee(user)

 The logic makes sense, the method here says it should work aswell? See this.

I am running this in ScriptRunner's console, just for testing purpose. 

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Aug 22, 2018 in Marketplace Apps

How a Marketplace app tech team is achieving gender diversity

Hello! My name is Genevieve Blanch, and I'm the Marketing Manager at RefinedWiki, creators of apps to give teams the tools to customize Atlassian platforms. Currently, 44% of the tech team at Re...

510 views 3 18
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you