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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-12-2017 05:40 PM
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.
- Labels:
-
Enterprise Asset Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-12-2017 09:00 PM
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.
But lets create a new one that we want to be "3 business days". It doesn't need to be more than this:
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.
- (function(){
- //Get the relative duration for 3 business days
- var relDur = 'f3ae5fc70f0132004cf365ba32050eb9';
- //get the incidents that we want to go through
- var encQue = 'active=true^resolved_atISNOTEMPTY';
- var gr = new GlideRecord('incident');
- gr.addEncodedQuery(encQue);
- gr.query();
- while(gr.next()){
- //Calculate and see if resolve date is more than 3 business days ago. And if, close the ticket.
- var dc = new DurationCalculator();
- //Load the schedule into our calculation through the function below
- addSchedule(dc);
- //Do the calculation and see if end date is before today
- dc.setStartDateTime(gr.resolved_at);
- if (!dc.calcRelativeDuration(relDur)) {
- gs.error("*** calcRelativeDuration failed for record {0}", gr.number);
- }
- if (dc.getEndDateTime() < gs.nowDateTime()){
- gr.setValue('state', 7);
- gr.update();
- }
- }
- function addSchedule(durationCalculator) {
- // Load the "8-5 weekdays" schedule into our duration calculator.
- var scheduleName = "8-5 weekdays";
- var grSched = new GlideRecord('cmn_schedule');
- grSched.addQuery('name', scheduleName);
- grSched.query();
- if (!grSched.next()) {
- gs.error("*** Could not find schedule {0}.", scheduleName);
- return;
- }
- return durationCalculator.setSchedule(grSched.getUniqueValue(), "GMT");
- }
- })();
That it, I hope it can give some ideas how to use the duration calculator for more fun stuff.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2018 04:03 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2018 06:57 PM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2018 07:19 PM
sorry to be a bit dim but do you mean the scheduled job i am creating? or something else entirely?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2018 08:24 PM
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?