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

@tulasi8 

did you see what came in gs.info() logs?

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

Hi @tulasi8 ,

Did you try creating incidents by impersonating any users in Eastern time zone?

vignesh parthib
Tera Guru

Hi @tulasi8 

 

The method setTZ() does not exist in the GlideDateTime API in ServiceNow. That’s why you're facing issue

This approach manually adjusts for timezone using milliseconds. Please refer Below script

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

    // Manually calculate US/Eastern offset (adjust for DST if needed)
    var easternOffsetHours = -4; // Use -5 if not in DST
    var offsetMillis = easternOffsetHours * 60 * 60 * 1000;

    // Get current UTC time
    var nowUTC = new GlideDateTime();

    // Convert to Eastern time by subtracting offset
    var nowEasternMillis = nowUTC.getNumericValue() + offsetMillis;

    var easternDate = new GlideDateTime();
    easternDate.setNumericValue(nowEasternMillis);

    // Get date part
    var dateStr = easternDate.getDate(); // yyyy-MM-dd

    // Construct start and end of day in Eastern time
    var startEastern = new GlideDateTime(dateStr + ' 00:00:00');
    var endEastern = new GlideDateTime(dateStr + ' 23:59:59');
	gs.info('Start time:'+startEastern+' & '+ 'End Time'+endEasterna);

    // Convert back to UTC for querying
    var startUTC = new GlideDateTime();
    startUTC.setNumericValue(startEastern.getNumericValue() - offsetMillis);

    var endUTC = new GlideDateTime();
    endUTC.setNumericValue(endEastern.getNumericValue() - offsetMillis);

    // Query incidents
    var gr = new GlideAggregate('incident');
    gr.addEncodedQuery('sys_domain=xyz^sys_created_on>=' + startUTC.getValue() + '^sys_created_on<=' + endUTC.getValue() + '^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);

 

Thanks,
Vignesh"If this solution resolves your issue, kindly mark it as correct."

@vignesh parthib 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

Hi @tulasi8 

You can change your time zone in your user profile to US/Eastern, then create multiple incidents using a background script. After that, check the system logs to verify whether the event was triggered as expected

Thanks,
Vignesh

"If this solution resolves your issue, kindly mark it as correct."