How to Assign a request sequentially in group members for an assignment group

Amol Pawar
Tera Guru

Hi Experts,

 

I have a requirement to create logic where I have a custom table and assign requests submitted by a record producer to one person using assignment rules.

 

Suppose I have one assignment group with four members. If the first request is assigned to Person 1, the second request should be assigned to Person 2. The third request should be assigned to Person 3, and the fourth request should be assigned to Person 4. The next request should be assigned to Person 1 again.

 

Let me know if anyhow we can achieve this!

Thanks in advance,

Amol

7 REPLIES 7

Sanjay191
Tera Sage

Hello @Amol Pawar 
Please Use the Round Robin Approached for this and refer the below videos for the better understanding and let me know if anything else.
https://www.youtube.com/watch?v=wp8uyWkcNPI


in this video it will works for  all incidents you need to put condition for your assignment group.
If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Thank You


Hi @Sanjay191,

Thank you for your reply.

 

This video was helpful. Only change I want is I want to assign the requests for the members in one Assignment group. 

I'm pasting the BR below, please let me know the changes I need to do if I want to assign these requests only to the members of one group.

 

(function executeRule(current, previous /*null when async*/ ) {

    var userTicketCount = {};
    var leastAssignedUser = null;
    var leastCount = 100;

    var inc = new GlideRecord('x_infte_rmp_rmp_task');
    inc.query();
    while (inc.next()) {
        var assignedTo = inc.assigned_to.toString();
        if (assignedTo) {
            if (!userTicketCount[assignedTo]) {
                userTicketCount[assignedTo] = 0;
            }
            userTicketCount[assignedTo]++;
        }
    }
    //find the user with the least count
    for (var userId in userTicketCount) {
        if (userTicketCount[userId] < leastCount) {
            leastCount = userTicketCount[userId];
            leastAssignedUser = userId;
        }
    }

    if (leastAssignedUser) {
        current.assigned_to = leastAssignedUser;
        gs.addInfoMessage("Least Assigned Ticket-->" + leastAssignedUser);
    }
})(current, previous);
 
Let me know if someone has any thoughts!
Thank you,
Amol

Hello @Amol Pawar 

Please use the below code and preform you task according i just use the incident table you can use your custom table and your assignment group sys_id.

I created After Insert BR on Incident table you use the befor BR also

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here

    assignedUser();

    function getUserFromGroup() {
        var groupSysId = "8a4dde73c6112278017a6a4baf547aa7"; // Replace with your group sys_id

        // Step 2: Create an object to hold user task counts
        var userTaskCount = {};

        // Step 3: Get all users in the group
        var groupMembers = new GlideRecord('sys_user_grmember');
        groupMembers.addQuery('group', groupSysId);
        groupMembers.addQuery('user.active', true); // Only active users
        groupMembers.query();
        while (groupMembers.next()) {
            var userName = groupMembers.user.toString(); // Get the user_name
            userTaskCount[userName] = 0; // Initialize task count to 0 for each user
        }

        var taskGR = new GlideRecord('incident');
        taskGR.addQuery('assignment_group', '8a4dde73c6112278017a6a4baf547aa7');
        taskGR.addQuery('assigned_to', 'IN', Object.keys(userTaskCount)); // Filter tasks assigned to group users
        taskGR.query();
        while (taskGR.next()) {
            var assignedUserName = taskGR.assigned_to.toString();
            if (userTaskCount.hasOwnProperty(assignedUserName)) {
                userTaskCount[assignedUserName]++; // Increment the count for the user
            }
        }

        // Step 5: Log or return the userTaskCount object
        gs.info("User task counts: " + JSON.stringify(userTaskCount));
        return userTaskCount;

    }

    function assignedUser() {
        // Step 1: Define the user task count JSON
        var getUserTaskCount = getUserFromGroup();
        gs.print("getUserTaskCount::" + getUserTaskCount);

        // Step 2: Find the user with the least task count
        var leastAssignedUser = '';
        var leastCount = 99999999999999; // Start with a high number

        for (var userName in getUserTaskCount) {
            // gs.print("USERNAME::"+userName);
            if (getUserTaskCount[userName] < leastCount) {

                leastCount = getUserTaskCount[userName];
                leastAssignedUser = userName;
            }
        }

        // Step 3: Assign the task to the least assigned user and increment their count
        if (leastAssignedUser) {
           
            current.setDisplayValue('assigned_to',leastAssignedUser);
            // current.assigned_to.setDisplayValue(leastAssignedUser); // Assign task to the user
            getUserTaskCount[leastAssignedUser]++; // Increment their task count

            // Log the updated task count
            gs.info("Task assigned to: " + leastAssignedUser);
            gs.info("Updated User Task Counts: " + JSON.stringify(getUserTaskCount));
        } else {
            gs.error("No users available to assign the task.");
        }

    }
    current.update();

})(current, previous);

If this helps you please mark my answer as helpful or accepted solution.
Thank You!!!!

Hello @Amol Pawar 
I'm Sure this will work for you 
If this helps you please mark my answer as helpful or accepted solution.
Thank You