Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Auto close Requested Items

Wayne Richmond
Tera Guru

I am trying to create a Business Rule that auto closes Requested Items (we ignore Requests) if they have not been updated whilst in a Resolved state for 5 days. There is a BR for Incidents which works fine, using the System Property 'glide.ui.autoclose.time'. I tried to recreate this script for ReqItems but it just doesn't work. Here is the code:



// This script automatically closes request items that are resolved
// and haven't been updated in the specified number of days.

autoCloseReqItems();

function autoCloseReqItems() {
var ps = gs.getProperty('glide.ui.autoclose.time');
var pn = parseInt(ps);

if (pn > 0) {
var gr = new GlideRecord('sc_req_item');
gr.addQuery('state', '4');
gr.addQuery('u_review_required', 'false');
gr.addQuery('sys_updated_on', '<', gs.daysAgoStart(pn));
gr.query();
while(gr.next()) {
gr.state = '3';
gr.comments = 'Request automatically closed after ' + pn + ' days in the Resolved state.';
gr.active = false;
gr.update();
}
}
}


Any ideas why this doesn't work?

7 REPLIES 7

neetusingh
Giga Guru

autoCloseReqItems();
function autoCloseReqItems() {
var pn = 5;
if (pn > 0) {
var gr = new GlideRecord('sc_req_item');
gr.addQuery('state', '4');
gr.addQuery('u_review_required', 'false');
gr.addQuery('sys_updated_on', '<', gs.daysAgoStart(pn));
gr.query();
while(gr.next()) {
gr.state = '3';
gr.comments = 'Request automatically closed after ' + pn + ' days in the Resolved state.';
gr.active = false;
gr.update();
}}}


Thanks. I've changed it. I'll see if it works tomorrow.


Hello Neetu


I tried implementing the code you suggested (for Service Catalog Item) - except that I'm trying to do this for Defects. Wherein if a defect has been in a 'Fixed' state for 5 days it should automatically close out.


Here is the code I'm trying to implement.


I implemented this as a Scheduled Job. However, when i click 'Execute Now', the defects that are in a 'Fixed' state don't close out). Can you please suggest a workaround.


Thanks!


Nomaan



// This script automatically closes defects that are in a Fixed state   for a specified number of days.


// This number is a property in System Properties.


// To place a comment in the incident, uncomment the "gr.comments" line.


// Fixed state (value = 15), closed state (value = 😎



autoCloseDefects();



function autoCloseDefects() {


  var ps = gs.getProperty('glide.ui.autoclose.time');


  var pn = 5;


 


  if (pn > 0) {


      var gr = new GlideRecord('rm_defect');


      gr.addQuery('state', '8');


      gr.addQuery('sys_updated_on', '<', gs.daysAgoStart(pn));


      gr.query();


      while(gr.next()) {


          gr.state = '15';


  //   gr.comments = 'Defect automatically closed after ' + pn + ' days in the Fixed state.';


          gr.active = false;


          gr.update();


      }


  }


}


geoffcox
Giga Guru

You called this a business rule. I think you need this to be a scheduled job. Otherwise, what makes it fire?

And you shouldn't have to wait until tomorrow to test your job. Just Click on "Execute Now".