Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,298,415
Community Members
 
Community Events
165
Community Groups

How do i get first assignee of issue using scriptrunner.

Edited

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

Hi @Surendar Singh 

You can look into the change history using ScriptRunner.

Take a look at this post here.

Ravi

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;

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

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)

 

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

Good to know that :)

0 votes

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
TAGS
Community showcase
Published in Jira Software

Upcoming changes to epic fields in company-managed projects

👋 Hi there Jira Community! A few months ago we shared with you plans around renaming epics in your company-managed projects. As part of these changes, we highlighted upcoming changes to epics on...

14,860 views 37 49
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you