Incident Created from last 2 min
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2023 04:46 AM
Hello experts,
I have written below but it's not working as excepted can anyone please guide me on this?
thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2023 05:07 AM
Hi @Mark Wood ,
To get the count of incidents created in the last 2 minutes. Here's a sample script that achieves this:
(function executeRule(current, previous /*, g */ ) {
var now = new GlideDateTime();
// Get the specified count and time frame in seconds
// Set the time interval for the query (2 minutes)
var specifiedCount = 2;
var timeFrameSeconds = specifiedCount * 60 * 1000;
// Calculate the date and time 2 minutes ago
var MinutesAgo = new GlideDateTime();
MinutesAgo.subtract(timeFrameSeconds);
// Create a GlideRecord for the Incident table
var incidentGR = new GlideRecord('incident');
// Add a condition to filter incidents created in the last 2 minutes
incidentGR.addQuery('sys_created_on', '>', MinutesAgo);
incidentGR.addQuery('sys_created_on', '<=', now);
// Execute the query to get the count of incidents
incidentGR.query();
var incidentCount = incidentGR.getRowCount();
// Set the count in a field or use it as needed
gs.addInfoMessage("count of Incident: " + incidentCount);
// Check if the incident count exceeds the specified count
if (incidentCount > specifiedCount) {
// Raise an alert or take appropriate actions here
gs.info('Incident count exceeds the specified count: ' + incidentCount);
}
})(current, previous);
If this helped you in any way, please hit the like button/mark it helpful. Also, don't forget to accept it as a solution. So it will help others to get the correct solution.
regards,
Prasad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2023 05:15 AM
Hi @Mark Wood ,
Hope you are doing great.
PLease try using below script to calculate the incidents created last 2 minute :
(function executeRule(current, previous /*, g */) {
// Get the specified count and time frame in seconds
var specifiedCount = 2;
var timeFrameSeconds = 120;
// Get the current date and time in UTC format
var now = new GlideDateTime();
// Create a new GlideDateTime for the time frame limit (120 seconds ago)
var timeFrameLimit = new GlideDateTime();
timeFrameLimit.addSeconds(-timeFrameSeconds);
// Query for incidents created within the time frame
var incidentGr = new GlideRecord('incident');
incidentGr.addQuery('sys_created_on', '>=', timeFrameLimit);
incidentGr.query();
// Count the number of incidents created within the time frame
var incidentCount = incidentGr.getRowCount();
// Check if the incident count exceeds the specified count
if (incidentCount > specifiedCount) {
// Raise an alert or take appropriate actions here
gs.info('Incident count exceeds the specified count: ' + incidentCount);
}
})(current, previous);
Regards,
Riya Verma