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.
12 REPLIES 12

Ankur Bawiskar
Tera Patron
Tera Patron

@tulasi8 

is this code in dev or prod?

if in prod then was this not tested in lower environments?

💡 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

@Ankur Bawiskar Before moving it to prod, i tested it in test instance, changed my preference to US/eastern and created 25 incidents through background script and tested it's working fine and triggering the notification only once. So, we moved it to prod but in prod it's not triggering in right weay . please help on this 

Ankur Bawiskar
Tera Patron
Tera Patron

@tulasi8 

try this and test it in DEV

(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 today's date in Eastern Time
    var easternDate = nowEastern.getLocalDate().toString();

    // Start of day in Eastern Time
    var startEastern = new GlideDateTime();
    startEastern.setDisplayValue(easternDate + ' 00:00:00');
    startEastern.setTimeZone(easternTZ);

    // End of day in Eastern Time
    var endEastern = new GlideDateTime();
    endEastern.setDisplayValue(easternDate + ' 23:59:59');
    endEastern.setTimeZone(easternTZ);

    // Convert to UTC for query
    var startUTC = startEastern.getValue();
    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) {
            // Optional: Check if event already fired today for this caller
            gs.eventQueue('it.checkmate.incident.limit.reached', current);
        }
    }

})(current, previous);

💡 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

@Ankur Bawiskar Before moving it to prod, i tested it in test instance, changed my preference to US/eastern and created 25 incidents through background script and tested it's working fine and triggering the notification only once. So, we moved it to prod but in prod it's not triggering in right weay . please help on this