It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

1 vote
Answer 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
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Posted Oct 09, 2018 in Jira Core

How to manage many similar workflows?

I have multiple projects that use variations of the same base workflow. The variations depend on the requirements of the project or issue type. The variations mostly come in the form of new statuses ...

501 views 6 0
Join discussion

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