Why doesn't user task assignment from scriptrunner work?

Dmitrii Strukov February 20, 2023

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.

1 answer

1 accepted

1 vote
Answer accepted
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 21, 2023

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.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events