Update all issues with last assignee

Hi All,

  • Is there any custom groovy script or function which will allow issues based on a JQL query to modify its assignee field value to its last assignee field value per issue ?
  • Please share your thoughts on this.

 

Regards

Chander Inguva

1 answer

1 accepted

Hi All,

         This is the tested and verified code for assigning previous value to assignee field based on a JQL Search.

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.*;
import com.atlassian.jira.issue.history.ChangeItemBean;
import com.atlassian.jira.issue.changehistory.*;
import com.atlassian.jira.issue.changehistory.ChangeHistory;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.project.Project;
import com.atlassian.jira.project.ProjectManager;
import com.atlassian.jira.user.util.UserManager;
import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.user.ApplicationUsers;
import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.bc.issue.search.SearchService;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.user.util.UserUtil;
import com.atlassian.jira.web.bean.PagerFilter;
 
jqlSearch = "project = <Project_Name> AND issuetype = Story and status = Open and assignee is empty"
SearchService searchService = ComponentAccessor.getComponent(SearchService.class);
UserUtil userUtil = ComponentAccessor.getUserUtil();
User user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
//Managers
IssueManager issueManager = ComponentAccessor.getIssueManager();
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
ChangeHistoryManager changeHistoryManager = ComponentAccessor.getChangeHistoryManager();
ProjectManager projectManager = ComponentAccessor.getProjectManager();
IssueFactory issueFactory = ComponentAccessor.getIssueFactory();
MutableIssue mIssue = issueFactory.getIssue();
List<Issue> issues = null;
SearchService.ParseResult parseResult =  searchService.parseQuery(user, jqlSearch)
if (parseResult.isValid()) 
{
    def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
    issues = searchResult.issues.collect {issueManager.getIssueObject(it.id)}
    for (Issue issue  : issues)
	{
        List<ChangeItemBean> assigneeChHistory = changeHistoryManager.getChangeItemsForField(issue, "assignee");                             
        if (!assigneeChHistory.isEmpty()) 
		{
		    ChangeItemBean assigneeChItemBean = assigneeChHistory.get(assigneeChHistory.size() - 1);
     	    previousAssignee = assigneeChItemBean.getFrom();
            userManager = (UserManager)  ComponentAccessor.getUserManager()
     		User usera = userManager.getUser(previousAssignee);
            issue.setAssignee(usera);      
            issue.store();
		}
	}
}

Hope this helps.

 

Regards

Chander Inguva

Suggest an answer

Log in or Join to answer
Community showcase
Teodora [Botron]
Published Feb 15, 2018 in Marketplace Apps

Jira Inferno: The Nine Circles of Jira Administration Hell

If you spend enough time as a Jira admin - whether you are managing a single, mid-sized instance, a large enterprise one or juggling multiple instances at once - you will eventually find yourself in ...

1,035 views 6 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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot