How to auto close Requested item after 7 days same as incident

Chaitanya Redd1
Tera Guru

Hello Team,

I'm trying to set auto close a request after 7 days. I have written business rule and schedule job but didn't worked can any one help me how to close the request.

Scheduled Job :

var query = 'state=4^sys_updated_onRELATIVELT@dayofweek@ago@7'; //state is resolved
var gr = new GlideRecord('table name');
gr.addEncodedQuery(query);
gr.query();
while(gr.next()){
gr.state = '5'; //set the state to closed
gr.update();
}
 
Business rule is same as incident one for autoclose.
 
Thanks,
Chaitanya
1 ACCEPTED SOLUTION

zulhiyatiuwi
Tera Expert

I have the same 

1. Create BR: When to run is after but without any condition

and the script. This request have the same time with incident. if you want to change it, just add your time in var ps = 7;

autoCloseRequest();

function autoCloseRequest() {
    var ps = gs.getProperty('glide.ui.autoclose.time');
    var pn = parseInt(ps);
    var queryTime = new GlideDateTime();
    queryTime.addDaysUTC(-pn);

    if (pn > 0) {
        var gr = new GlideRecord('sc_req_item');
        gr.addQuery('state', '5');
        gr.addQuery('sys_updated_on', '<', queryTime);
        gr.addQuery('u_coh', 'false');
        while (gr.next()) {
            gr.state = '3';
            gr.comments = 'Request ditutup otomatis setelah ' + pn + ' hari dari tiket diselesaikan.';
            gr.active = false;
            gr.update();
        }
    }
}

 

And then create the scheduled job. fcScriptName=request autoclose, is the BR that create before. and it will run every 1 hour.

this is working fine in the instance. hope this will give you little help.

View solution in original post

6 REPLIES 6

zulhiyatiuwi
Tera Expert

I have the same 

1. Create BR: When to run is after but without any condition

and the script. This request have the same time with incident. if you want to change it, just add your time in var ps = 7;

autoCloseRequest();

function autoCloseRequest() {
    var ps = gs.getProperty('glide.ui.autoclose.time');
    var pn = parseInt(ps);
    var queryTime = new GlideDateTime();
    queryTime.addDaysUTC(-pn);

    if (pn > 0) {
        var gr = new GlideRecord('sc_req_item');
        gr.addQuery('state', '5');
        gr.addQuery('sys_updated_on', '<', queryTime);
        gr.addQuery('u_coh', 'false');
        while (gr.next()) {
            gr.state = '3';
            gr.comments = 'Request ditutup otomatis setelah ' + pn + ' hari dari tiket diselesaikan.';
            gr.active = false;
            gr.update();
        }
    }
}

 

And then create the scheduled job. fcScriptName=request autoclose, is the BR that create before. and it will run every 1 hour.

this is working fine in the instance. hope this will give you little help.

Ankush Jangle1
Kilo Guru

Hello,

 

Write a Schedule job for this requirement which runs every day

var query = 'state=-4^sys_updated_onRELATIVELT@dayofweek@ago@7'; //state is resolved
var gr = new GlideRecord('table_name');
gr.addEncodedQuery(query);
gr.query();

while(gr.next()){
gr.state = '3'; //set the state to closed
gr.update();
}

 

 

Please Mark it helpful/Correct if it helps you

Namrata Khabale
Giga Guru

Hey Chaitanya,

 

In addition to above post refer the Link, it might help you:

 

https://medium.com/servicenow-techminds/how-to-auto-close-resolved-incidents-after-7-days-in-service...

 

Mark Correct and Helpful if you find my response worthy!!!

 

Best Regards,

Namrata.

I think the previous developer get the technic from this website. Its literally same. šŸ˜„