Assignment rule scripting based on Central time

Sohini Kar
Tera Expert
Routing Requirement:
  • All incidents and requests for type "corporate" received between 6:00 AM and 5:00 PM Central Time should be assigned to: Group1
  • Any requests outside of this time window should be assigned to: Group2

I created two assignemnt groups. But the incident raised through record producer is getting assigned to Group 1 only even though I am raising the request on outside business hours. Can someone help me with the exact script?

 

I used this script, but it is not giving correct result:

(function() {

var nowCT = new GlideDateTime();

nowCT.setTZ('America/Chicago');

var hour = parseInt(nowCT.getHourLocal());

 

return (hour < 6 || hour > 17);

})();

6 REPLIES 6

vaishali231
Tera Guru

hey @Sohini Kar 

Updated Working Script

 script  Assignment Rule:

(function() {
   var gdt = new GlideDateTime();
   // Convert current time to Central Time
   var ctTime = gdt.getDisplayValueInternal('America/Chicago');
   var hour = parseInt(ctTime.split(' ')[1].split(':')[0]);
   // Business hours: 6 AM to 5 PM
   if (hour >= 6 && hour <= 17) {
       current.assignment_group.setDisplayValue('Group1');
   } else {
       current.assignment_group.setDisplayValue('Group2');
   }
})();

 

Recommended Best Practice 

Instead of hardcoding time logic, use a Schedule, which is more reliable and handles daylight saving automatically.

Steps:

  1. Create a schedule (e.g., Corporate Hours)

Time: 6:00 AM to 5:00 PM

Timezone: America/Chicago

Use this script:

 (function() {
var schedule = new GlideSchedule('<schedule_sys_id>');
   var now = new GlideDateTime();
   if (schedule.isInSchedule(now)) {
       current.assignment_group.setDisplayValue('Group1');
   } else {
       current.assignment_group.setDisplayValue('Group2');
   }
})();

*************************************************************************************************************************************

If this response helps, please mark it as Accept as Solution and Helpful.

Doing so helps others in the community and encourages me to keep contributing.

Regards

Vaishali Singh

 

This is actually partially working. Now the assignment group is being displayed only. But it is not getting assigned to that particular group.
I want it to be assigned to that particular group.

@Sohini Kar 

check my below comment and use code in record producer script

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

Ankur Bawiskar
Tera Patron

@Sohini Kar 

simply handle this using record producer script

Why not use some schedule which customer already has?

Script using GlideSchedule

var schedule = new GlideSchedule();
schedule.load('PUT_SCHEDULE_SYS_ID_HERE');

var now = new GlideDateTime();

// Outside business hours
if (!schedule.isInSchedule(now)) {
    current.assignment_group = 'group2SysId'; // Assign to Group2
} else {
    current.assignment_group = 'group1SysId'; // Assign to Group 1
}

If you don't want to use schedule then use this script

var strConvertedDateTime = new GlideScheduleDateTime(new GlideDateTime()).convertTimeZone("UTC", "CET");
var gdtConvertedDateTime = new GlideDateTime(strConvertedDateTime);

var hours = gdtConvertedDateTime.toString().split(' ')[1].split(":")[0];
if (hours >= 6 && hours <= 17) {
    // within business hours
    current.assignment_group = 'group2SysId'; // Assign to Group2
} else {
    current.assignment_group = 'group1SysId'; // Assign to Group 1
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader