Script for Round Robin

Tamilvanan T
Tera Contributor

Created a Business rule script for Round Robin availability for a certain assignment group on the HR payroll cases. Also, made the Round Robin active for the users in the group, But when creating a case and updating the case the Round Robin rule is not applied. Is there anything we need to check in ServiceNow apart from Round robin active check. 

 

Please look into the below script 

 

(function executeRule(current, previous /*null when async*/ ) {
   
    current.assigned_to = resolveTech();

    function resolveTech() {
        var tech_arr = [];
        var gr = new GlideRecord('sys_user_grmember');
        gr.addQuery('group', current.assignment_group);
        gr.query();
        while (gr.next()) {
            var isAssignable = gr.user.u_round_robbin_active;
            var technician = gr.user + "";
            var lastAssigned = gr.user.u_last_assigned + "";
       if (isAssignable == true) {                       // validate users in the group availability
                tech_arr.push({sys_id: technician,last_assigned: lastAssigned}); // push user and date/time in array
            }
        }
        tech_arr.sort(function(a, b) {                 //sort array
            return a.last_assigned < b.last_assigned ? -1 : a.last_assigned > b.last_assigned ? 1 : 0;
        });
        updateDateTime(tech_arr[0].sys_id); //updating timestamp on ticket at the top of the array
        return tech_arr[0].sys_id; //assigned_to technician , at the top of the array
    }

    function updateDateTime(assigned) {           // Update Time Stamp on user table, called back in the sort array
        var nowDate = new GlideDateTime().getDisplayValue(); //Use GlideDateTime instead: new GlideDateTime().getDisplayValue()
        var updateDate = new GlideRecord('sys_user');
        updateDate.addQuery('sys_id', assigned);
        updateDate.query();
        while (updateDate.next()) {
            updateDate.u_last_assigned = nowDate;
            updateDate.update();
        }
    }

})(current, previous);
 
Kindly let me know if anything is missing or needs to be updated. Please advise.
3 REPLIES 3

its_SumitNow
Kilo Sage

Hi @Tamilvanan T 

I can see a few potential issues with your setup and script. Let me walk you through the key areas to check:

Areas to Verify:

1. Business Rule Configuration

  • When to run: Ensure it's set to run on "before" since you're updating current.assigned_to
  • Insert: Should be checked if you want it to run on new records
  • Update: Check this only if the assignment group changes (add condition: assignment_group CHANGES)
  • Filter Conditions: Verify your conditions properly target HR payroll cases with the specific assignment group
  • Order: Check the execution order - lower numbers run first (default is 100)
  • Active: Confirm the Business Rule itself is checked as Active

2. Table Configuration

  • Verify the Business Rule is created on the correct table (likely sn_hr_core_case for HR cases, not the standard incident table)

3. Field Name Verification

  • Confirm the custom field is named u_round_robbin_active (note: you have "robbin" instead of "robin" - verify this matches your actual field name)
  • Verify u_last_assigned exists on the sys_user table

4. Script Issues to Address:

(function executeRule(current, previous) {
    
    // Add a condition to only run when assignment group is populated and changes
    if (!current.assignment_group) {
        return;
    }
    
    current.assigned_to = resolveTech();
    
    function resolveTech() {
        var tech_arr = [];
        var gr = new GlideRecord('sys_user_grmember');
        gr.addQuery('group', current.assignment_group);
        gr.addQuery('user.u_round_robbin_active', true); // Filter at query level
        gr.addQuery('user.active', true); // Also check if user is active
        gr.query();
        
        while (gr.next()) {
            var technician = gr.user.sys_id + ""; // Use sys_id explicitly
            var lastAssigned = gr.user.u_last_assigned + "";
            
            tech_arr.push({
                sys_id: technician,
                last_assigned: lastAssigned || '1970-01-01 00:00:00' // Handle null values
            });
        }
        
        // Check if array has users
        if (tech_arr.length === 0) {
            gs.addErrorMessage('No available users in assignment group for Round Robin');
            return current.assigned_to; // Return existing value
        }
        
        // Sort array
        tech_arr.sort(function(a, b) {
            return a.last_assigned < b.last_assigned ? -1 : a.last_assigned > b.last_assigned ? 1 : 0;
        });
        
        updateDateTime(tech_arr[0].sys_id);
        return tech_arr[0].sys_id;
    }
    
    function updateDateTime(assigned) {
        var nowDate = new GlideDateTime();
        var updateDate = new GlideRecord('sys_user');
        updateDate.get(assigned); // Use .get() instead of query for single record
        updateDate.u_last_assigned = nowDate;
        updateDate.update();
    }
    
})(current, previous);

Additional Debugging Steps:

  1. Add logging to verify the rule executes:
gs.info('Round Robin BR executed for case: ' + current.number);
gs.info('Assignment Group: ' + current.assignment_group);
gs.info('Tech array length: ' + tech_arr.length);
  1. Check System Logs: Navigate to System Logs > System Log > All after creating/updating a case to see if there are any errors

  2. Verify Group Membership: Confirm users are actually members of the assignment group in User Administration > Groups

  3. Test the field: Manually verify u_round_robbin_active is set to true for at least one user in the group

  4. Check ACLs: Ensure the Business Rule has permission to update the assigned_to field

Common Pitfalls:

  • Running on "after" instead of "before"
  • Conflicting Business Rules with higher priority overwriting your assignment
  • Assignment Group field being cleared/changed by another rule
  • No users having u_round_robbin_active = true

Try these fixes and let me know if the issue persists. Adding debug logging will help identify exactly where the process is breaking down.

 

if my response helps ,kindly mark it as helpful & accept it as solution 🙂

Warm Regards

Sumit 

Technical Consultant

Prathmeshda
Mega Guru

Hello @Tamilvanan T 
ServiceNow Round Robin Assignment – Business Rule Script Reference

Script Overview

This Business Rule (BR) is designed to:

  1. Identify all users in a specific assignment group (current.assignment_group) who are marked as available for Round Robin (u_round_robbin_active = true).

  2. Sort these users by their u_last_assigned timestamp.

  3. Assign the case to the user who was assigned the longest time ago.

  4. Update the user’s u_last_assigned timestamp to the current time.

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

    current.assigned_to = resolveTech();

    function resolveTech() {
    var tech_arr = [];
    var gr = new GlideRecord('sys_user_grmember');
    gr.addQuery('group', current.assignment_group);
    gr.query();
    while (gr.next()) {
    var isAssignable = gr.user.u_round_robbin_active;
    var technician = gr.user + "";
    var lastAssigned = gr.user.u_last_assigned + "";
    if (isAssignable == true) { // Validate users in the group availability
    tech_arr.push({sys_id: technician, last_assigned: lastAssigned});
    }
    }

    // Sort array by last assigned timestamp
    tech_arr.sort(function(a, b) {
    return a.last_assigned < b.last_assigned ? -1 : a.last_assigned > b.last_assigned ? 1 : 0;
    });

    updateDateTime(tech_arr[0].sys_id); // Update timestamp for user
    return tech_arr[0].sys_id; // Assign to user at the top of the sorted array
    }

    function updateDateTime(assigned) {
    var nowDate = new GlideDateTime().getDisplayValue();
    var updateDate = new GlideRecord('sys_user');
    updateDate.addQuery('sys_id', assigned);
    updateDate.query();
    while (updateDate.next()) {
    updateDate.u_last_assigned = nowDate;
    updateDate.update();
    }
    }

    })(current, previous);

    Business Rule Configuration

    • Table: Ensure the BR is on the HR Case table or the correct table.

    • When: Typically before insert or before update.

    • Active: BR must be active.

    • Condition: Check if any condition is filtering out records (e.g., only certain HR cases).

    • Advanced: Ensure the script is set as Advanced.

      User Table Fields

      • u_round_robbin_active must be a boolean on sys_user.

      • u_last_assigned must be a Date/Time field on sys_user.

      • Ensure all users in the assignment group have these fields populated.

        Script Logic

        • gr.user + "" converts a GlideElement to a string; better to use gr.user.sys_id directly.

        • lastAssigned is currently a string. Date comparison might fail if empty or invalid format.

           

          GlideDateTime Usage

          • Current script uses getDisplayValue() for storing u_last_assigned. Better to store GlideDateTime object directly:

           

           
          updateDate.u_last_assigned = new GlideDateTime(); updateDate.update();


          gs.info('Case ' + current.number + ' assigned to ' + tech_arr[0].sys_id);



          If this response proves useful, please mark it as Accept as Solution and Helpful. Doing so benefits both the community and me. 👍🙂

           



OlaN
Tera Sage

Hi,

Not sure why you need this complicated script to resolve a Round Robin functionality, but did you check if you have other functions configured that would interfere with this script?

Things like Assignment rules or similar.

Did you check if the business rule is executing and processing as expected ?