Schedule job to run last working day of month

ulhask
Tera Contributor

Hi Folks,

I am working on a requirement where I need to run a schedule Report on last working day of the month.If anyone has implemented some same requirement in there project or came across around the logic for running report on last working day please help me.Thanks in advance.

I am testing with some logic for now as below:

var ans;

var td= new GlideDateTime("2018-02-29 12:00:00");

gs.print('todays date '+td);

var ndm=td.getDaysInMonthLocalTime();

gs.print('number of days in current month '+ndm);

else if(ndm==31)

{     if(td.getDayOfMonthLocalTime()==29)

      {

            if(td.getDayOfWeekLocalTime()==5)

            {

            ans=true; gs.print('31/29');

            }

      }

      if(td.getDayOfMonthLocalTime()==30)

      {

            if(td.getDayOfWeekLocalTime()==5)

            {

            ans=true; gs.print('31/30');

            }

      }

      else if(td.getDayOfMonthLocalTime()==31)

      {

            if(td.getDayOfWeekLocalTime()==1||td.getDayOfWeekLocalTime()==2||td.getDayOfWeekLocalTime()==3||td.getDayOfWeekLocalTime()==4||td.getDayOfWeekLocalTime()==5)

            {

            ans=true;   gs.print('31/31');

            }

      }  

}

else if(ndm==30)

{     if(td.getDayOfMonthLocalTime()==28)

      {

            if(td.getDayOfWeekLocalTime()==5)

            {

            ans=true;   gs.print('30/28');

            }

      }

      if(td.getDayOfMonthLocalTime()==29)

      {

            if(td.getDayOfWeekLocalTime()==5)

            {

            ans=true; gs.print('30/29');

            }

      }

      else if(td.getDayOfMonthLocalTime()==30)

      {

            if(td.getDayOfWeekLocalTime()==1||td.getDayOfWeekLocalTime()==2||td.getDayOfWeekLocalTime()==3||td.getDayOfWeekLocalTime()==4||td.getDayOfWeekLocalTime()==5)

            {

            ans=true; gs.print('30/30');

            }

      }  

}

else if(ndm==28)

{     if(td.getDayOfMonthLocalTime()==26)

      {

            if(td.getDayOfWeekLocalTime()==5)

            {

            ans=true; gs.print('28/26');

            }

      }

      if(td.getDayOfMonthLocalTime()==27)

      {

            if(td.getDayOfWeekLocalTime()==5)

            {

            ans=true;   gs.print('28/27');

            }

      }

      else if(td.getDayOfMonthLocalTime()==28)

      {

            if(td.getDayOfWeekLocalTime()==1||td.getDayOfWeekLocalTime()==2||td.getDayOfWeekLocalTime()==3||td.getDayOfWeekLocalTime()==4||td.getDayOfWeekLocalTime()==5)

            {

            ans=true;   gs.print('28/28');

            }

      }  

}

else if(ndm==29)

{     if(td.getDayOfMonthLocalTime()==27)

      {

            if(td.getDayOfWeekLocalTime()==5)

            {

            ans=true;   gs.print('29/27');

            }

      }

      if(td.getDayOfMonthLocalTime()==28)

      {

            if(td.getDayOfWeekLocalTime()==5)

            {

            ans=true; gs.print('29/28');

            }

      }

      else if(td.getDayOfMonthLocalTime()==29)

      {

            if(td.getDayOfWeekLocalTime()==1||td.getDayOfWeekLocalTime()==2||td.getDayOfWeekLocalTime()==3||td.getDayOfWeekLocalTime()==4||td.getDayOfWeekLocalTime()==5)

            {

            ans=true;   gs.print('29/29');

            }

      }  

}

4 REPLIES 4

ulhask
Tera Contributor

Here's a quick function I came up with to determine if it's the last working day of the month. It basically just adds 1 day (Monday-Thursday) to see if the month changes, or adds 3 days to Friday and checks if the month changes. If it does, then it's the last working day of the month. Sample use of the function on the last three lines>. Sorry about the grotesque indentation. Bad copy/paste on this community software.



function isLastWorkingDay(gdt) {



var today = gdt.getDayOfWeekLocalTime();


var thisMonth = gdt.getMonthLocalTime();



switch (today) {


// Saturday and Sunday are NOT the last working day of the month


case 6:


case 7:


return false;


break;



// If it's a Friday, add 3 and see if the month changes.


case 5:


var nextMonday = gdt;


nextMonday.addDaysLocalTime(3);


var nextMondayMonth = nextMonday.getMonthLocalTime();


return (nextMondayMonth != thisMonth);


break;



// If it's a Mon-Thu add one and see if the month changes


default:


var nextDay = gdt;


nextDay.addDaysLocalTime(1);


var nextDayMonth = nextDay.getMonthLocalTime();


return (nextDayMonth != thisMonth);


break;


}



}




var now = new GlideDateTime();


var lwd = isLastWorkingDay(now);


gs.info('Last working day=' + lwd);


Hi @Chuck Tomasi, can you help me utilize this script on a sys_template, to generate a ticket on the last business day of each month?  I've been playing with it in the Condition for the schedule of the template, however, it is not running.  The community does not seem to be able to help (See https://community.servicenow.com/community?id=community_question&sys_id=031757fadb20d510be625ac2ca9619fd) and support has already pushed me away since it is a customization.

The breaks show an unreachable code error, so I tried it with and without them.  No luck either way.

find_real_file.png

function isLastWorkingDay(gdt) {

    var today = gdt.getDayOfWeekLocalTime(); 
    var thisMonth = gdt.getMonthLocalTime();
    
    switch (today) {
    // Saturday and Sunday are NOT the last working day of the month
    case 6:
    case 7:
    return false;

    break;
     
    // If it's a Friday, add 3 and see if the month changes.
    case 5:
    var nextMonday = gdt;
    nextMonday.addDaysLocalTime(3);
    var nextMondayMonth = nextMonday.getMonthLocalTime();
    return (nextMondayMonth != thisMonth);
 
    break;
    
    // If it's a Mon-Thu add one and see if the month changes
    default: 
    var nextDay = gdt;
    nextDay.addDaysLocalTime(1);
    var nextDayMonth = nextDay.getMonthLocalTime();
    return (nextDayMonth != thisMonth);
    
    break;
    }
}
     
    var now = new GlideDateTime();
    var lwd = isLastWorkingDay(now);
    gs.info('Last working day=' + lwd);
    
    

 

Here is what the Schedule Job looks like, no script shown, but clicking Configure Job Definition shows the script.find_real_file.png

 

Thanks!

dvp
Mega Sage
Mega Sage

Here is the script that considers holidays as well



var sched = new GlideSchedule('08fcd0830a0a0b2600079f56b1adb9ae'); // Specify the schedule sys_id


var d = new GlideDateTime();


var total_days = d.getDaysInMonth();


var abc = false;


var last_business_day = '';




d.setDayOfMonth(total_days);




while(!abc){


  abc = sched.isInSchedule(d);


   


  if(!abc)


          d.addDays(-1);


}




var gdt = new GlideDateTime().getDate();


var business_day = new GlideDateTime(d).getDate();




var diff = gs.dateDiff(gdt, business_day, true)/86400;




diff == 0