How do i get first assignee of issue using scriptrunner.

Surendar Singh November 20, 2019

I need to get first assignee (when a task is assigned by project lead and when it is rejected by QA team, it must auto-assign to  the first assignee of the task ),I am using scriptrunner by using script but i am not able to get value.

2 answers

1 accepted

0 votes
Answer accepted
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.
November 20, 2019

Hi @Surendar Singh 

You can look into the change history using ScriptRunner.

Take a look at this post here.

Ravi

Surendar Singh November 20, 2019

Hello Ravi,

I appreciate for your answer, but code is not in deprecated and getting issue.

i am attaching my code please provide solution.

 

import com.atlassian.jira.issue.*
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.core.util.DateUtils
import com.atlassian.jira.component.ComponentAccessor
com.atlassian.jira.issue.changehistory.ChangeHistoryManager

def componentAccessor = ComponentAccessor.getInstance();
def changeHistoryManager = componentAccessor.getChangeHistoryManager();

def changeItems = changeHistoryManager.getChangeHistories(issue);
return changeItems.get(changeItems.size()-1).getAuthorObject()
def firstAssigned;
if(changeItems !=null && !changeItems.isEmpty()){
ChangeItemBean ci = (ChangeItemBean) changeItems.get(0);
String assigneeName = ci.getFrom(); // name
String assigneeFullName = ci.getFromString();// full name
def assignedTime = ci.getCreated().getTime();
firstAssigned = assignedTime;

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.
November 20, 2019

Hi @Surendar Singh 

Please try the code below. It will first retrieve the first assignee of the issue and then assign the issue to that first assignee.

import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser


IssueManager im = ComponentAccessor.getIssueManager();
MutableIssue issue = im.getIssueObject("ANDROID-41");


def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
def firstAssignee=changeHistoryManager.getChangeItemsForField(issue, "assignee")
def firstNotNullAssignee = firstAssignee[0]?.from ?: firstAssignee[0]?.to

//return firstNotNullAssignee

ApplicationUser user = ComponentAccessor.getUserManager().getUserByName(firstNotNullAssignee.toString())

def issueService = ComponentAccessor.getIssueService()

def validateAssignResult = issueService.validateAssign(user, issue.id, issue.reporterId)
issueService.assign(user, validateAssignResult)

Please note: I have not included all the checks in the code like what will happen if there is no assignee of the issue or if user doesn't exist anymore but I hope this code will give you good starting point.

Thanks. Let me know if it helps.

Ravi 

Like Surendar Singh likes this
Surendar Singh November 21, 2019

Thanks Ravi

It worked for me and i have made some changes and code is working now. I am attaching my code here:

import com.atlassian.jira.issue.*
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.core.util.DateUtils
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.history.ChangeItemBean

def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
def firstAssignee=changeHistoryManager.getChangeItemsForField(issue, "assignee")
def firstNotNullAssignee = firstAssignee[0]?.from ?: firstAssignee[0]?.to

//return assignee
issue.setAssigneeId(firstNotNullAssignee)

 

Surendar Singh November 21, 2019

Thanks Ravi

It worked for me and i have made some changes and code is working now. I am attaching my code here:

import com.atlassian.jira.issue.*
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.core.util.DateUtils
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.history.ChangeItemBean

def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
def firstAssignee=changeHistoryManager.getChangeItemsForField(issue, "assignee")
def firstNotNullAssignee = firstAssignee[0]?.from ?: firstAssignee[0]?.to

//return assignee
issue.setAssigneeId(firstNotNullAssignee)
Like Ravi Sagar _Sparxsys_ likes this
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.
November 21, 2019

Good to know that :)

0 votes
Rahul Sharma May 25, 2022

I am new to JIRA. Can you please suggest that where this piece of code will be written 

Suggest an answer

Log in or Sign up to answer