Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Notification is triggering multiple times

tulasi8
Tera Contributor

Hi Community,

 

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.

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, This ensures that the notification is triggered correctly when 25 incidents are created within the same calendar day as per US/Eastern time. But it is not working as expected. Sometimes it's triggering when 16th notification is created , sometimes its triggering when 25th notification is created . Sometimes it's triggering more than once in a single day .. 

 

Can anyone please help on this?

 

Conditions : after insert , domain is xyz, caller is xyz

Script :

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

    var easternTZ = 'US/Eastern';

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

    // Get date part only (yyyy-MM-dd)
    var easternDate = nowEastern.getLocalDate().toString();
   
    var startEastern = new GlideDateTime(easternDate + ' 00:00:00');
    startEastern.setTimeZone(easternTZ);
   

    var endEastern = new GlideDateTime(easternDate + ' 23:59:59');
    endEastern.setTimeZone(easternTZ);
    gs.print(endEastern);
    var startUTC = startEastern.getValue(); // UTC string
    var endUTC = endEastern.getValue();
   

    // Query incidents
    var gr = new GlideAggregate('incident');
    gr.addEncodedQuery('sys_domain=a028216e47ed51508b6b4537536d4383^sys_created_on>=' + startUTC + '^sys_created_on<=' + endUTC + '^caller_id=748b5c7f97071550d2ec73100153af73');
    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);
 
 
Regards,
Prasanna.
14 REPLIES 14

@Ankur Bawiskar  Currently, the event is being triggered twice in production yesterday with previous script , whereas it should only trigger once when the specified conditions are met. Would it be feasible to implement a flag within the script to verify whether the event has already been triggered for the day? I f yes can u please give script 

tulasi8
Tera Contributor

@Ankur Bawiskar please help on this.

tulasi8
Tera Contributor

@Dr Atul G- LNG @Ravi Gaurav @Hemanth M1  can anyone please help

@tulasi8 

where will you maintain the flag?

also if the logic is correct in DEV then it should work fine in TEST, UAT, PROD

💡 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  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

MaxMixali
Tera Guru

Hello i hope that can help

 

 

(function executeRule(current, previous) {

var easternTZ = "America/New_York"; // Recommended name

// Current time in UTC
var now = new GlideDateTime();

// Convert to Eastern Time
var nowEastern = new GlideDateTime(now);
nowEastern.setTZ(easternTZ);

// Extract yyyy-MM-dd in Eastern time
var dateStr = nowEastern.getLocalDate().toString();

// Build start and end of the Eastern day (always correct!)
var startOfDayEastern = new GlideDateTime(dateStr + " 00:00:00");
startOfDayEastern.setTZ(easternTZ);
var endOfDayEastern = new GlideDateTime(dateStr + " 23:59:59");
endOfDayEastern.setTZ(easternTZ);

// Convert both to UTC for querying sys_created_on
var startUTC = startOfDayEastern.getValue();
var endUTC = endOfDayEastern.getValue();

// Count incidents
var ga = new GlideAggregate('incident');
ga.addQuery('sys_domain', 'a028216e47ed51508b6b4537536d4383');
ga.addQuery('caller_id', '748b5c7f97071550d2ec73100153af73');
ga.addQuery('sys_created_on', '>=', startUTC);
ga.addQuery('sys_created_on', '<=', endUTC);
ga.addAggregate('COUNT');
ga.query();

if (ga.next()) {
var count = parseInt(ga.getAggregate('COUNT'), 10);

// Fire *once*, when hitting 25
if (count === 25) {
gs.eventQueue('it.checkmate.incident.limit.reached', current);
}
}

})(current, previous);

 

 

If you find this answer useful, please mark it as solution accepted/helpful

Massimiliano Micali