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

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

@harshchaudh 

there is an OOTB data policy which blocks INC from update when State = 7

So you should set Resolution notes and Resolution code while updating

AnkurBawiskar_0-1753097646744.png

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');
        gr.close_code = 'Solution provided';
        gr.close_notes = 'Closed via script';
        gs.info(gr.number);

    }

}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

It didn't worked.

 

@harshchaudh 

it should work.

please share what didn't work.

You mentioned it's not getting updated so I assumed your filter and query is fine

Remember closing incident requires those 2 fields to be updated and hence I updated those while updating the record.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

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