I would like to create a scheduled job to close resolved records (incident, u_request & sc_req_item) after 5 business days.

Phil O_shea
Mega Contributor

Hi Gang,

 

I have been trolling through community posts in regards to achieving this.

I found one but it has not quite worked for me as it it is quite specific to the users needs.

I need something a bit more generic to target a wider range of record types.

This is what i have and feel free to tell me how wrong i am ha ha (still learning). 🙂  

 

Run: Periodically

Repeat Interval: 0days,01hours

Conditional: true

--------------------------------------------------------------------

condition:

function notweekend() {

      var now = new Date();

      var day = now.getDay();

      var result = false;

if(day != 6 || day != 7) {

result = true;

}

return result;

}

notweekend();

-----------------------------------------------------------------

Run script:  

 

function autocloserecords() {

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('incident' || 'u_request' || 'sc_req_item');

gr.addQuery('state', State.Resolved);

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

gr.query();

while(gr.next()) {

gr.state = State.Closed;

gr.active = false;

gr.closed_by = gr.resolved_by;

gr.update();

 

}

 

}

 

}

----------------------------------------------------------------------------

Regards,

Phil.

10 REPLIES 10

Mwatkins
ServiceNow Employee
ServiceNow Employee

One note about your script, GlideRecord constructor does not accept multiple table names.


Also, instead of using "gr" as a variable name I recommend something unique, since it can often cause variable naming conflicts in sub-scopes where the "gr" variable value gets overwritten by some other script. I know the documentation uses "gr" in a lot of examples, but it can cause problems in the real world.



Instead of this:


var gr = GlideRecord("incident" || "u_request" || "sc_req_item");


...



Do this:


var closedTasks = new GlideRecord("task");


closedTasks.addQuery("sys_class_name", ["incident", "u_request", "sc_req_item"]);


...