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

Aditya Telideva
ServiceNow Employee
ServiceNow Employee

First step is to create a relative duration that fits the amount of days you want. You can easy find it under System Scheduler->Schedules->Relative Durations.


As you can see there are a few examples that you can either use or look to understand how it's build up.


find_real_file.png


But lets create a new one that we want to be "3 business days". It doesn't need to be more than this:


find_real_file.png


Copy the sys_id of this record, since you will be needing it later for the coding of the schedule job.



Next step will just to get the sys_id of the schedule you want to use. Just head to the schedule record and copy that sys_id as well.



Now we got all the nice things we need. So lets head to System Definition->Scheduled Job and press New. Choose "Automatically run a script of your choosing" and we get to the new record.


Here you can set how often you want it to run, but perhaps once a day would be nice and let's pick a time when we don't think so many people are working.


Remember to put all your code in an anonymous function so it variable etc. doesn't get messed up with other things that is running. If you don't do this, strange things can happen if you for example have two jobs running at the same time and both having a variable called for example gr.



Summary of the code below.


  • Get the sys_id for the relative duration I want to use
  • Query all the records I want to go through. Active is true and got a resolve date is the ones I want here.
  • Loop through the records I get and check if its been more than 3 business days since it was set to resolve. If it is, close it.




  1. (function(){  
  2.   //Get the relative duration for 3 business days  
  3.   var relDur = 'f3ae5fc70f0132004cf365ba32050eb9';  
  4.   //get the incidents that we want to go through  
  5.   var encQue = 'active=true^resolved_atISNOTEMPTY';  
  6.  
  7.   var gr = new GlideRecord('incident');  
  8.   gr.addEncodedQuery(encQue);  
  9.   gr.query();  
  10.  
  11.   while(gr.next()){  
  12.   //Calculate and see if resolve date is more than 3 business days ago. And if, close the ticket.  
  13.   var dc = new DurationCalculator();  
  14.  
  15.   //Load the schedule into our calculation through the function below  
  16.   addSchedule(dc);  
  17.  
  18.   //Do the calculation and see if end date is before today  
  19.   dc.setStartDateTime(gr.resolved_at);  
  20.   if (!dc.calcRelativeDuration(relDur)) {  
  21.   gs.error("*** calcRelativeDuration failed for record {0}", gr.number);  
  22.   }  
  23.   if (dc.getEndDateTime() < gs.nowDateTime()){  
  24.   gr.setValue('state', 7);  
  25.   gr.update();  
  26.   }  
  27.   }  
  28.  
  29.   function addSchedule(durationCalculator) {  
  30.   //   Load the "8-5 weekdays" schedule into our duration calculator.  
  31.   var scheduleName = "8-5 weekdays";  
  32.   var grSched = new GlideRecord('cmn_schedule');  
  33.   grSched.addQuery('name', scheduleName);  
  34.   grSched.query();  
  35.   if (!grSched.next()) {  
  36.   gs.error("*** Could not find schedule {0}.", scheduleName);  
  37.   return;  
  38.   }  
  39.   return durationCalculator.setSchedule(grSched.getUniqueValue(), "GMT");  
  40.   }  
  41.  
  42. })();  


That it, I hope it can give some ideas how to use the duration calculator for more fun stuff.


Hello Aditya,



Thankyou so much for the detailed response and also i am sorry for not replying on this thread earlier as we have been flat chat since before Christmas.



Can you please elaborate on "Next step will just to get the sys_id of the schedule you want to use. Just head to the schedule record and copy that sys_id as well."



I am attempting to put this together this week.



TIA.



Phil.


Sure Phil, i meant you gotto get the sys_id of the schedule you might want to use. you can use g_form and g_form.getUniqueValue() will give you the current sys_id.


I hope this helps a bit. And once you consolidate, please let me know so that i can help you more


Thank you,


Aditya Telidevara



PS: Please do mark as "Helpful", in case you found so


sorry to be a bit dim but do you mean the scheduled job i am creating? or something else entirely?


Actually all good i found the bit i needed.


So i did an 'execute now and it didn't do anything, is there a step i might have missed?