How to schedule a report for last sunday of every month

Rahul Raja Sami
Tera Guru

How to schedule  a report for last sunday of every month

var gdt = new GlideDateTime(dt);
gs.print(gdt);
var day = gdt.getDayOfWeek();
var days = gdt.getDayOfMonthLocalTime();
gs.print(day); 
gs.print(days); 
   if (day == 7 && (days >=25)) {
        result = true;
    } else {
        result = false;
    }
gs.print(result);

 

but this is not totally correct.

help me out

1 ACCEPTED SOLUTION
3 REPLIES 3

bammar
Kilo Sage
Kilo Sage

Easy way = send on 1st day of every month but im sure for business reasons we have to stick to last Sunday of mth...

Easier non elegant way- determine the next years DAY OF YEAR for the 12 last sundays and hard code them in- then you have a year to figure it out

I think unless there is a javascript method to get the last day of the current month- taking into account leap year as well shorter months( why the days>25 may not work) then it may be hard to be sure about this- other way is to mark on your schedule in servicenow last sunday of the mth somehow then have this check schedule.

I think there is a way for sure to program this and will like to see it. 

Ian Mildon
Tera Guru

This script will determine if it is the last "working day" of the month. So you should be able to modify it to check for the last Sunday of the month:

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);