Autoclose Incident excluding weekends after 2 days of being resolved
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-22-2023 04:32 AM
Hi All,
I have created a custom table in scoped application called "Service Desk Incidents" which is extended from Task table.I have a requirement for Auto Close these SD incidents after 2 business days(excluding sat and sun) of being resolved.
I have created a scheduled job like below in "sysauto_script" table.
updateRecords();
function updateRecords(){
try{
var inc = new GlideRecord('sd_incident');
inc.addQuery('state', 150);
inc.query();
while(inc.next()){
var start = new GlideDateTime(inc.u_resolved_at);
var nowTime = new GlideDateTime();
var days = getDateDiffExcWeekends(start,nowTime);
if(days >=2){
inc.state = 7;
inc.incident_state = 7;
inc.active = false;
inc.update();
}
}
}
catch(ex){
gs.log("scheduled job " + ex);
}
}
function getDateDiffExcWeekends(start , nowTime){
var days = 0;
while (start < nowTime) {
start.addDaysUTC(1);
if (start.getDayOfWeekUTC() != 6 && start.getDayOfWeekUTC() != 7){
days++ ;
}
}
return days;
}
The issue I'm facing here is, when I click on execute now, this script is closing all the incidents which are in state resolved for 2 days. It is not excluding the weekends.
Can anyone please guide me on this.
Thank you in Advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-22-2023 05:27 AM
HI @Nagashree5 ,
I trust you are doing great.
Looking at your code, it seems that the logic to exclude weekends is not working as expected. Let's modify the getDateDiffExcWeekends function to correctly exclude Saturdays and Sundays. Replace your existing function with the updated code below:
function getDateDiffExcWeekends(start, nowTime) {
var days = 0;
var oneDay = 1000 * 60 * 60 * 24; // One day in milliseconds
while (start.getNumericValue() < nowTime.getNumericValue()) {
start.addDaysUTC(1);
// Check if the current day is not Saturday (6) and not Sunday (0)
if (start.getDayOfWeekUTC() !== 6 && start.getDayOfWeekUTC() !== 0) {
days++;
}
}
// Adjust the days count based on milliseconds difference
days -= Math.floor((nowTime.getNumericValue() - start.getNumericValue()) / oneDay);
return days;
}
Was this answer helpful?
Please consider marking it correct or helpful.
Your feedback helps us improve!
Thank you!
Regards,
Amit Gujrathi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-27-2023 07:02 AM