Inbound Action Script - DST

Tom Lapkowicz
Tera Expert

I am working on setting up an Inbound Action that kicks off an Incident.  I am on EST.

 

If the email that kicks off the Inbound Action is received between M-F 8am-4pm then I want the ticket to be assigned to the ATM Settlement group, if it comes in any other time then it should go to the ATM Services group.

 

I put the script below together (with assistance from ChatGPT).  It kicks off the incident correctly but it is still a bit off with the time.  For example, when I ran it at around 10:30am on a Thursday in September, when I look in the logs it said the current hour was 9 and that DST in effect was False (we are currently on DST)

 

Any idea what the issue may be with the script?  Thank you.

 

var useremail = email.from_sys_id;
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', useremail);
gr.query();
if (gr.next()) {
    var priorityValue = new calculateAndSetPriority();
   
    // Use JavaScript Date object to get the current hour and day
    var date = new Date();
    var utcHour = date.getUTCHours(); // Get current hour in UTC

    // Determine if Daylight Saving Time (DST) is in effect
    var isDST = (date.getTimezoneOffset() < -300); // Eastern Time DST is UTC-4 (240 mins offset), else UTC-5 (300 mins offset)

    // Adjust for ET, accounting for DST
    var utcOffset = isDST ? -4 : -5; // Use UTC-4 for DST, UTC-5 otherwise
    var currentHour = (utcHour + utcOffset + 24) % 24; // Adjust for ET, ensure it's always positive
    var currentDay = (date.getDay() + 6) % 7 + 1; // Convert JavaScript day (0 = Sunday) to ServiceNow day (1 = Sunday)

    // Log to confirm current local time
    gs.log("ATMSet Voicemail - Test: Local current hour (ET): " + currentHour);
    gs.log("ATMSet Voicemail - Test: Local current day: " + currentDay);
    gs.log("ATMSet Voicemail - Debug: DST in effect: " + isDST);

    current.priority = priorityValue.calculateAndSetPriority(1, 1);
    current.category = 'atm_services_and_mobile_deposit';
    current.subcategory = 'Production Issues';
    current.impact = '1';
    current.urgency = '1';
    current.caller_id = '36992b5c1b7016105a48ca65604bcbfe'; // ATM Set
    current.location = '1cd65f24c3ae46908d2b6c3bb00131d2';
    current.contact_type = 'email';
    current.short_description = email.subject;
    current.description = "received from: " + email.origemail + "\n\n" + email.body_text;

    if (currentHour >= 8 && currentHour < 16 && currentDay >= 2 && currentDay <= 6) {
        // During business hours (8 AM - 4 PM Eastern Time, Monday to Friday)
        current.assignment_group = '7eb27171c3e41e5404362af0e00131f2'; // ATM Settlement
    } else {
        // Outside of business hours (4 PM - 8 AM Eastern Time, or on weekends)
        current.assignment_group = '64d6e252db83d3004c7b55384b9619d5'; // ATM Services
    }

    current.insert();
}
1 ACCEPTED SOLUTION

Tai Vu
Kilo Patron
Kilo Patron

Hi @Tom Lapkowicz 

Let give GlideDateTime and GlideTime a try. It will give you the amount of time that daylight saving time is offset.

Sample below. The Eastern DST in 2024 starts from March 10, 2024 at 2 a.m. local time

Timi_1-1726821398244.png

 

So in your script, the day and time validation part could be adjusted as following.

var gt = new GlideTime();
var gdt = new GlideDateTime();
var currentHour = gt.getHourLocalTime();
var currentDay = gdt.getDayOfWeekLocalTime();

if (currentHour >= 8 && currentHour < 16 && currentDay >= 2 && currentDay <= 6) {
	current.assignment_group = '7eb27171c3e41e5404362af0e00131f2'; // ATM Settlement
} else {
	current.assignment_group = '64d6e252db83d3004c7b55384b9619d5'; // ATM Services
}

 

Let me know if it works for you.

 

Cheers,

Tai Vu

View solution in original post

2 REPLIES 2

Community Alums
Not applicable

Hey there,

 

Check out this thread, it seems to have the same problem you are trying to solve: https://www.servicenow.com/community/itsm-forum/auto-assign-assignment-group-based-on-time-of-day-sc...

 

You'll just need to create your own schedule and be sure to update that schedule name in the script on 2nd line of the method ifScript.

 

~Nick

Tai Vu
Kilo Patron
Kilo Patron

Hi @Tom Lapkowicz 

Let give GlideDateTime and GlideTime a try. It will give you the amount of time that daylight saving time is offset.

Sample below. The Eastern DST in 2024 starts from March 10, 2024 at 2 a.m. local time

Timi_1-1726821398244.png

 

So in your script, the day and time validation part could be adjusted as following.

var gt = new GlideTime();
var gdt = new GlideDateTime();
var currentHour = gt.getHourLocalTime();
var currentDay = gdt.getDayOfWeekLocalTime();

if (currentHour >= 8 && currentHour < 16 && currentDay >= 2 && currentDay <= 6) {
	current.assignment_group = '7eb27171c3e41e5404362af0e00131f2'; // ATM Settlement
} else {
	current.assignment_group = '64d6e252db83d3004c7b55384b9619d5'; // ATM Services
}

 

Let me know if it works for you.

 

Cheers,

Tai Vu