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 Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

80 views 0 5
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