The CreatorCon Call for Content is officially open! Get started here.

Change the time zone to trigger the notification

tulasi8
Tera Contributor

In our current setup, the ServiceNow instance operates in the IST (Indian Standard Time) zone. A business rule was implemented to monitor incident creation for a specific domain. The rule triggers a notification when 25 incidents are created in a single day for a specific caller.

Initially, the rule was designed to evaluate incidents based on IST. However, since our clients operate in the US/Eastern time zone, the notification logic needed to be aligned with their local time.

To address this, the business rule was updated to calculate the start and end of the day based on US/Eastern time, and convert those values to UTC for accurate querying. This ensures that the notification is triggered correctly when 25 incidents are created within the same calendar day as per US/Eastern time.

 

SCRIPT IN BUSINESS RULE :

 

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

// Get current date/time in US/Eastern
var easternTZ = 'America/New_York';
var now = new GlideDateTime();
now.setTZ(easternTZ);

// Get beginning and end of "today" in US/Eastern
var startOfDay = new GlideDateTime(now);
startOfDay.setDisplayValueInternal(startOfDay.getDate() + ' 00:00:00');
startOfDay.setTZ(easternTZ);

var endOfDay = new GlideDateTime(now);
endOfDay.setDisplayValueInternal(endOfDay.getDate() + ' 23:59:59');
endOfDay.setTZ(easternTZ);

// Convert to UTC for querying
var startUTC = startOfDay.getValue(); // returns UTC time
var endUTC = endOfDay.getValue();

var gr = new GlideAggregate('incident');
gr.addEncodedQuery('sys_domain=xyz^sys_created_on>=' + startUTC + '^sys_created_on<=' + endUTC + '^caller_id=yyy');
gr.addAggregate('COUNT');
gr.query();

if (gr.next()) {
var count = parseInt(gr.getAggregate('COUNT'), 10);
if (count == 25) {
gs.eventQueue('it.checkmate.incident.limit.reached', current);
}
}

})(current, previous);

 

but it's not working, can anyone please help on this 

10 REPLIES 10

Ankur Bawiskar
Tera Patron
Tera Patron

@tulasi8 

try this and add gs.info() for debugging

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

    var easternTZ = 'America/New_York';

    // Get current time in Eastern Time
    var nowEastern = new GlideDateTime();
    nowEastern.setTZ(easternTZ);

    // Get date part only (yyyy-MM-dd)
    var easternDate = nowEastern.getLocalDate().toString();

    // Construct start and end of day in Eastern Time
    var startEastern = new GlideDateTime(easternDate + ' 00:00:00');
    startEastern.setTZ(easternTZ);

    var endEastern = new GlideDateTime(easternDate + ' 23:59:59');
    endEastern.setTZ(easternTZ);

    // Convert to UTC for querying
    var startUTC = startEastern.getValue(); // UTC string
    var endUTC = endEastern.getValue();

    // Query incidents
    var gr = new GlideAggregate('incident');
    gr.addEncodedQuery('sys_domain=xyz^sys_created_on>=' + startUTC + '^sys_created_on<=' + endUTC + '^caller_id=yyy');
    gr.addAggregate('COUNT');
    gr.query();

    if (gr.next()) {
        var count = parseInt(gr.getAggregate('COUNT'), 10);
        if (count === 25) {
            gs.eventQueue('it.checkmate.incident.limit.reached', current);
        }
    }

})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@Ankur Bawiskar can you please help like how to do the testing part for this because , my system is in IST, if i create incidents manualy it will create by ist time zone

@tulasi8 

try changing your timezone to Eastern from preferences and see

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@Ankur Bawiskar notification is not getting triggered