Notification is triggering multiple times
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2025 11:34 PM
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 :
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2025 01:55 AM
var TIMEZONE = "US/Eastern";
var DOMAIN = "a028216e47ed51508b6b4537536d4383";
var LIMIT = 25;
var easternNow = new GlideDateTime();
easternNow.setTZ(TIMEZONE);
var easternDate = easternNow.getDate().getDisplayValue(); // yyyy-MM-dd
var startEastern = new GlideDateTime(easternDate + " 00:00:00");
startEastern.setTZ(TIMEZONE);
var endEastern = new GlideDateTime(easternDate + " 23:59:59");
endEastern.setTZ(TIMEZONE);
var startUTC = startEastern.getValue();
var endUTC = endEastern.getValue();
var ga = new GlideAggregate("incident");
ga.addEncodedQuery(
"sys_domain=" + DOMAIN +
"^sys_created_on>=" + startUTC +
"^sys_created_on<=" + endUTC
);
ga.groupBy("caller_id");
ga.addAggregate("COUNT");
ga.query();
while (ga.next()) {
var caller = ga.getValue("caller_id");
var count = parseInt(ga.getAggregate("COUNT"));
if (count >= LIMIT && !alreadyTriggered(caller, easternDate)) {
gs.eventQueue("it.checkmate.incident.limit.reached", ga);
markTriggered(caller, easternDate);
}
}
// ---------------------
// Helper Functions
// ---------------------
function alreadyTriggered(callerID, dateString) {
var gr = new GlideRecord('incident');
gr.addQuery("caller_id", callerID);
gr.addQuery("u_date", dateString);
gr.query();
return gr.hasNext();
}
function markTriggered(callerID, dateString) {
var gr = new GlideRecord('incident');
gr.initialize();
gr.caller_id= callerID;
gr.u_date = dateString;
gr.insert();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2025 05:57 AM
Hi @tulasi8 ,
please find the corrected script below
(function executeRule(current, previous /*null when async*/) {
var easternTZ = 'US/Eastern';
// Current time in Eastern Time
var nowEastern = new GlideDateTime();
nowEastern.setTimeZone(easternTZ);
// Get Eastern date string (yyyy-MM-dd)
var easternDate = nowEastern.getLocalDate().toString();
// Start and end of day in Eastern Time
var startEastern = new GlideDateTime(easternDate + ' 00:00:00');
startEastern.setTimeZone(easternTZ);
var endEastern = new GlideDateTime(easternDate + ' 23:59:59');
endEastern.setTimeZone(easternTZ);
// Convert to UTC for query
var startUTC = startEastern.getValue();
var endUTC = endEastern.getValue();
// Query incidents for this caller and domain within Eastern day
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) {
// Prevent duplicate triggers: check if event already fired today
var eventCheck = new GlideRecord('sysevent');
eventCheck.addQuery('name', 'it.checkmate.incident.limit.reached');
eventCheck.addQuery('parm1', current.caller_id.toString());
eventCheck.addQuery('sys_created_on>=', startUTC);
eventCheck.query();
if (!eventCheck.hasNext()) {
gs.eventQueue('it.checkmate.incident.limit.reached', current, current.caller_id.toString());
}
}
}
})(current, previous);
NOTE -
- Time Zone Handling: Start and end times are correctly converted to UTC for querying.
- Duplicate Prevention: Added a check in sysevent to ensure the event fires only once per day per caller.
- Strict Equality: Use count === 25 to avoid type coercion issues.
Thanks,
Rithika.ch