Auto close the resolved cases after 3 business days only for 8-5 weekdays

AishwaryaS1
Kilo Sage

I've requirement, in which I need to close the resolved cases after 3 business days.
But while doing that we need to consider the Business Hrs. are 8-5 and excluding weekends.
I was trying using scheduled job, it is working but including weekends and 24/7

Please let me know if anyone have solution for this.

Aishwarya Shelake
1 ACCEPTED SOLUTION

AishwaryaS1
Kilo Sage
updateRecords();
function updateRecords() {
    try {
        var gr = new GlideRecord('x_klla_fin_ops_sd_accounts_payable');
        gr.addQuery('state', 6);//resolved state=6
        gr.query();
        while (gr.next()) {

            var start = new GlideDateTime(gr.resolved_at);//resolved time
            var nowTime = new GlideDateTime();//current time

            var days = getDateDiffExcWeekends(start, nowTime);
            gs.info('days are: '+ days+ 'Case Number: '+gr.number);
            // if days more than 3
            if (days >= 3) {
                gr.state = 4;
                gr.comments = 'This case has been auto closed';
                gr.update();              
            }
        }
    } catch (ex) {
        gs.info(ex);
    }
}

function getDateDiffExcWeekends(start, end) {
    var days = 0;
    while (start < end) {
        start.addDaysUTC(1);

        if (start.getDayOfWeekUTC() != 6 && start.getDayOfWeekUTC() != 7) {
            days++;
        }
    }
    return days;
}
 
I've tried this solution and it works.
Aishwarya Shelake

View solution in original post

4 REPLIES 4

Runjay Patel
Giga Sage

Hi @AishwaryaS1 ,

 

Try using below script in your schedule job.

 

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

if(pn > 0){
   //Get a schedule by name to calculate duration
   var schedRec = new GlideRecord('cmn_schedule');
   schedRec.get('name', '8-5 weekdays excluding holidays');
   if (typeof GlideSchedule != 'undefined')
      var sched = new GlideSchedule(schedRec.sys_id);
   else
      var sched = new Packages.com.glide.schedules.Schedule(schedRec.sys_id);
   
   //Get the current date/time in correct format for duration calculation
   var actualDateTime = new GlideDateTime();
   actualDateTime.setDisplayValue(gs.nowDateTime());
   
   //Query for resolved incident records
   var gr = new GlideRecord('incident');
   gr.addQuery('state', 6);
   gr.query();
   while(gr.next()){
      //Close any records that have not been updated in 'pn' number of hours
      //Date difference calculated based on specified schedule
      var difDay = sched.duration(gr.sys_updated_on.getGlideObject(), actualDateTime).getDayPart()*24;
      var difHour = sched.duration(gr.sys_updated_on.getGlideObject(), actualDateTime).getDurationValue().split(':')[0].substr(-2);
      var dif = difDay + parseInt(difHour.replace(/^[0]+/g,""));
     
      if(dif >= pn){
         gr.state = 7;
         gr.active = false;
         //gr.comments = 'Incident automatically closed after ' + pn + ' hours in the Resolved state.';
         gr.update();
      }
   }
}

 

 

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

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

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

@Runjay Patel I tried this solution, but it is not working as expected. I want to close the resolved cases after 3 business days. Suppose case is resolved on Thursday then it should be closed on Tuesday not on Sunday.
I need to do this using scheduled job.

Aishwarya Shelake

Hi @AishwaryaS1 ,

 

I have given you working code, you just need to create schedule which includes holidays and run only weekdays.

AishwaryaS1
Kilo Sage
updateRecords();
function updateRecords() {
    try {
        var gr = new GlideRecord('x_klla_fin_ops_sd_accounts_payable');
        gr.addQuery('state', 6);//resolved state=6
        gr.query();
        while (gr.next()) {

            var start = new GlideDateTime(gr.resolved_at);//resolved time
            var nowTime = new GlideDateTime();//current time

            var days = getDateDiffExcWeekends(start, nowTime);
            gs.info('days are: '+ days+ 'Case Number: '+gr.number);
            // if days more than 3
            if (days >= 3) {
                gr.state = 4;
                gr.comments = 'This case has been auto closed';
                gr.update();              
            }
        }
    } catch (ex) {
        gs.info(ex);
    }
}

function getDateDiffExcWeekends(start, end) {
    var days = 0;
    while (start < end) {
        start.addDaysUTC(1);

        if (start.getDayOfWeekUTC() != 6 && start.getDayOfWeekUTC() != 7) {
            days++;
        }
    }
    return days;
}
 
I've tried this solution and it works.
Aishwarya Shelake