Font Size:
Ask Joget AI

Assign to User in Group with the least assignments

Introduction

Ensuring tasks are evenly distributed among participants is essential for workload balance in a business process. This documentation explains how to dynamically assign tasks to users within a group with the fewest open assignments. This can help optimize task allocation and prevent overloading any single participant.

How does it work?

This solution uses a Bean Shell script within the Map Participants to Users feature of Joget DX 8. The script retrieves all users from a specified group and checks their current open assignments. It then assigns the task to the user with the fewest open assignments. This method ensures that tasks are distributed based on current workloads.

import java.util.ArrayList;
import java.util.Collection;
import org.joget.apps.app.service.AppUtil;
import org.joget.directory.model.User;
import org.joget.directory.model.service.ExtDirectoryManager;

ExtDirectoryManager directoryManager = (ExtDirectoryManager) AppUtil.getApplicationContext().getBean("directoryManager");
workflowAssignmentDao = AppUtil.getApplicationContext().getBean("workflowAssignmentDao");

// set groupId
String groupId = "G-001";

// Get active users of the group, sorted by firstName
Collection userList = directoryManager.getUsers(null, null, null, null, groupId, null, "1", "firstName", false, null, null);

String assignTo = null;
int lowestAssignmentCount = -1;
Collection assignees = new ArrayList();

for (Object u : userList) {
    User user = (User) u;

    // Count this user's open assignments (all apps and processes).
    // Pass packageId / processDefId in the first arguments to narrow the count.
    int userAssignmentSize = workflowAssignmentDao.getAssignmentSize(null, null, null, null, user.getUsername(), "open");

    if (lowestAssignmentCount == -1 || userAssignmentSize < lowestAssignmentCount) {
        lowestAssignmentCount = userAssignmentSize;
        assignTo = user.getUsername();
    }
}

if (assignTo != null) {
    assignees.add(assignTo);
}
return assignees;

In this code, you will get all the users from the group with ID "G-001". Then, we will traverse through each user and obtain each user's assignment count.

The first user with the least assignment count will be picked as the assignee of the current assignment. You may use this as a reference to your future coding needs.

Related documentation

Created by Julieth Last modified by Nur Baizura Badrul Sham on Sep 30, 2026