Find Open Incidents which with no updates for last 30 days and closes them (without Encoded Query).

harshchaudh
Tera Contributor

var grd = new GlideDateTime("2025-07-21");

grd.addDays(-30);

grd.query();

var gr = new GlideRecord("incident");

gr.addQuery('sys_updated_on','<',grd);

gr.query();

while(gr.next()){

if(gr.state!='7'){

gr.setValue('state','7');

gs.info(gr.number);

}

}

i write this code can anyone please help me to correct this code ?? as it worked but doesnot change the state to closed of some incidents IDK why?

1 ACCEPTED SOLUTION

Chaitanya ILCR
Mega Patron

Hi @harshchaudh ,

 

try this

var grd = new GlideDateTime("2025-07-21 00:00:00");

grd.addDays(-30);

grd.query();

var gr = new GlideRecord("incident");

gr.addQuery('sys_updated_on', '<', grd);

gr.query();


if (gr.next()) {

    if (gr.state != '7') {

        gr.setValue('state',7);
        gr.setUseEngines(false);
		gr.setWorkflow(false);
		gr.update();

        gs.info(gr.number);

    }

}

 

why not use the Update JOB (no code approach)?

https://www.youtube.com/watch?v=5jZmZS6tb1I

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

View solution in original post

Welcome to our comprehensive tutorial on using Update Jobs and Delete Jobs in ServiceNow! In this video, we will walk you through the essential functionalities of these powerful features, enabling you to efficiently manage and maintain your ServiceNow instances. By the end of this video, you'll ...
8 REPLIES 8

SumanthDosapati
Mega Sage
Mega Sage

@harshchaudh 

Use this below script in background scripts.

var incGr = new GlideRecord("incident");
incGr.addActiveQuery();
incGr.addEncodedQuery("sys_updated_onRELATIVELT@dayofweek@ago@30");
incGr.query();
while (incGr.next()){
   incGr.state = 7;
   incGr.close_code = 'Solution provided';
   incGr.close_notes = 'Closed via background script';
   incGr.update();
}

 

Accept the solution and mark as helpful if it does, to benefit future readers.
Regards,
Sumanth

 

Sorry but i need to run it without encoded query.

@harshchaudh 

If you want to do it without encoded query, you can use below script

var incGr = new GlideRecord("incident");
incGr.addActiveQuery();
incGr.query();

// Calculate date 30 days ago
var thresholdDate = new GlideDateTime();
thresholdDate.subtractDays(30);

while (incGr.next()) {
   // Compare sys_updated_on with thresholdDate
   if (incGr.sys_updated_on.getGlideObject().compareTo(thresholdDate) < 0) {
      incGr.state = 7;
      incGr.close_code = 'Solution provided';
      incGr.close_notes = 'Closed via background script';
      incGr.update();
   }
}

 

Though this works, curious to know why you don't want to use encoded query.

 

Accept the solution and mark as helpful if it does, to benefit future readers.
Regards,
Sumanth

GaneshSuresh
Giga Guru

The line grd.query(); is incorrect because GlideDateTime doesn't have a query() method. You should remove this line.
Ensure that '7' corresponds to the closed state in your instance's state field for incidents. Different instances might have different state values for 'Closed'.
After setting a field value, you need to call gr.update(); to save the changes.

 

Here's the revised code:

var grd = new GlideDateTime("2025-07-21");
grd.addDays(-30);

var gr = new GlideRecord("incident");
gr.addQuery('sys_updated_on', '<', grd);
gr.query();

while (gr.next()) {
if (gr.state != '7') {
gr.setValue('state', '7');
gr.update(); // Save the changes
gs.info(gr.number + " state updated to closed.");
}
}