Incident Created from last 2 min

Mark Wood
Tera Contributor

Hello experts,

I have written below but it's not working as excepted can anyone please guide me on this?

thank you.

// Business Rule to check if incidents exceed a specified count within a specified time frame
(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();
gs.addInfoMessage("Line no 12 TimeFrameLimit"+timeFrameLimit);
  timeFrameLimit.addSeconds(-timeFrameSeconds);
 
 
gs.addInfoMessage("Line no 16 TimeFrameLimit "+timeFrameLimit);
 
  // 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 = 0;
  while (incidentGr.next()) {
    incidentCount++;
  }
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);

 

2 REPLIES 2

Community Alums
Not applicable

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

Riya Verma
Kilo Sage
Kilo Sage

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);

 

 
Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma