last X days incidents

sravankumar9637
Tera Contributor

how can i get last x days incidents in background scripts. 

6 REPLIES 6

jaheerhattiwale
Mega Sage
Mega Sage

@sravankumar9637 Please write below code in background script

var numberOfDays = 10;
var inc = new GlideRecord("incident");
inc.addQuery("sys_created_onRELATIVELT@dayofweek@ahead@"+numberOfDays);
inc.query();

while(inc.next()){
    gs.info(inc.number.toString());
}
 
Please mark as correct answer if this solves your issue.
Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

ok fine nice , But how to get without encoded Query

@sravankumar9637 Without encoded query means?

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

Laszlo Balla
ServiceNow Employee
ServiceNow Employee

Here's an example script you can use (obviously doing whatever you'd like in the while loop):

 

var daysAgo = 7; // update accordingly

var latestIncidents = new GlideRecord('incident');
latestIncidents.addEncodedQuery('sys_created_onRELATIVEGT@dayofweek@ago@' + daysAgo.toString());
latestIncidents.query();
while(latestIncidents.next()) {
  gs.print(latestIncidents.getValue('number'));
}