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
Community Members
Community Events
Community Groups

Round-robin assigning

I have a "simple" script for Round Robin assigning. And I have one problem:  changes made by the script running as post-function is not reflecting in the assignee field. How to fix it?)

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.user.util.UserManager;
import com.atlassian.jira.jql.parser.JqlQueryParser;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.bc.issue.IssueService.IssueResult;
import com.atlassian.jira.bc.issue.IssueService;
import com.atlassian.jira.issue.IssueInputParameters;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.web.bean.PagerFilter;
import java.sql.Timestamp;

class Agent {
ApplicationUser user;
int issueCount;
long lastAssignedTime;

def groups = ComponentAccessor.getGroupManager() as GroupManager
def agents = groups.getUsersInGroup("jira-software-users")

def userManager = ComponentAccessor.getUserManager() as UserManager
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser.class)
def searchProvider = ComponentAccessor.getComponent(SearchProvider.class)

def PriorityQueue<Agent> agentPQ = new PriorityQueue<>(agents.size(), new Comparator<Agent>() {
int compare(Agent o1, Agent o2) {
if(o1.issueCount == o2.issueCount){
if(o2.lastAssignedTime == o1.lastAssignedTime){
return o1.lastAssignedTime.compareTo(o2.lastAssignedTime)
return o1.issueCount - o2.issueCount

def jqlQuery = "status != Closed and assignee=null ORDER BY created DESC"
def query = jqlQueryParser.parseQuery(jqlQuery)
def results =, user, PagerFilter.getUnlimitedFilter())

assignAgent(user,results, agents, jqlQueryParser, searchProvider,agentPQ)

def assignAgent(ApplicationUser admin,SearchResults results ,Collection<ApplicationUser> agents, JqlQueryParser jqlQueryParser, SearchProvider searchProvider, PriorityQueue<Agent> agentPQ){
def issueManager = ComponentAccessor.issueManager
MutableIssue mutableIssue = issueManager.getIssueObject(results.issues.get(0).id)
agents.forEach{agent ->
log.debug("agent = " + agent)
Agent a = new Agent()

def count = getIssueCountForAgent(agent, jqlQueryParser , searchProvider)

if(count > 0){
a.lastAssignedTime = getLastAssignedTimeForAgent(agent, jqlQueryParser , searchProvider)
log.debug("Last assigned time for agent " + agent + "= " + a.lastAssignedTime)
a.issueCount = count

a.lastAssignedTime = 0
a.issueCount = 0
a.user = agent
issueManager.updateIssue(admin, mutableIssue, EventDispatchOption.ISSUE_ASSIGNED, true)
def issueService = ComponentAccessor.getIssueService()
IssueInputParameters params = ComponentAccessor.issueService.newIssueInputParameters()
params.assigneeId = agentPQ.peek()
def validateUpdateResult = issueService.validateUpdate(admin,,params)
log.debug("Assignee validated = " + validateUpdateResult.fieldValuesHolder.get("assigneeId"))
IssueService.IssueResult serviceResult=issueService.update(admin,validateUpdateResult)
log.debug validateUpdateResult.errorCollection.errorMessages.toString()

def getLastAssignedTimeForAgent(ApplicationUser agent, JqlQueryParser jqlQueryParser, SearchProvider searchProvider ){
def jqlQuery = "assignee=\""+ + "\" and status!=Closed "+ " ORDER BY created DESC"
def query = jqlQueryParser.parseQuery(jqlQuery)
log.debug("JQL Query = " + jqlQuery)
def results =, agent, PagerFilter.getUnlimitedFilter())
if(results.issues.size() == 0)
return 0;
def changeLogManager = ComponentAccessor.changeHistoryManager
def assignedAt = changeLogManager.getChangeItemsForField(results.issues.get(0),"assignee").find {
it.toString ==
}?.getCreated() as Timestamp
Date d = new Date(assignedAt.getTime())
return d.getTime()
return 0

def getIssueCountForAgent(ApplicationUser agent ,JqlQueryParser jqlQueryParser, SearchProvider searchProvider ){
def jqlQuery = "assignee=\""+ + "\" and status!=Closed "+ " ORDER BY created DESC"
def query = jqlQueryParser.parseQuery(jqlQuery)
def results =, agent, PagerFilter.getUnlimitedFilter())

0 answers

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in Jira Service Management

Jira Service Management Documentation Opportunities

Hello everyone, Hope everyone is safe! A few months ago we posted an article sharing all the new articles and documentation that we, the AMER Jira Service Management team created. As mentioned ...

253 views 0 6
Join discussion

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