Why doesn't user task assignment from scriptrunner work?
I use Jira 8.1 Data Center.
I'm trying to implement user assignment on a status transition (in a POST function) in a scriptrunner using the following code:
String issueKey = issue.getKey(); // current task
String username = "admin"; // The user to be assigned
IssueManager issueManager = ComponentAccessor.getIssueManager();
MutableIssue issueByCurrentKey = issueManager.getIssueByCurrentKey(issueKey);
ApplicationUser appUser = ComponentAccessor.getUserManager().getUserByName(username);
issueByCurrentKey.setAssignee(appUser); // Assign a user
ApplicationUser currentUser = ComponentAccessor.getUserManager().getUserByName( "admin" ); // The user performing the transition
def updatedIssue = issueManager.updateIssue(currentUser, issueByCurrentKey, EventDispatchOption.ISSUE_ASSIGNED, true); // issue update
println("new assignee: " + updatedIssue.getAssigneeUser()); // In this object displays the NEW USER
I display the executor in the console after updating the task and the new user is displayed there, but WHY IS IT NOT SAVE IN THE TASK?
I am simultaneously looking at this task in the database and it does not change there either (so it's not about indexing)
It works in Script Console, but not works on transition.
What am I doing wrong?
Updated the question as I didn't phrase it correctly in the first place.
In a transition post function, unless you have a reason to and you put your task at the end of all the other default tasks, do not call the updateIssue method.
You see, the built-in issue object that script runner gives you stays in memory until it is saved by the built-in post function.
So, what you are doing here is creating a copy of the memory issue object, then updating and saving the copy.
Then, the built-in post function will save the original issue object in memory (with still the original assignee) and overwrite your changes.
To change the assignee, just update the existing issue object:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
String username = "admin"; // The user to be assigned
ApplicationUser appUser = ComponentAccessor.userManager.getUserByName(username);
issue.assignee = appUser
The post function will make the update and the index and all the good stuff.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.