Scheduled script for last working day of the month

Rodzula
Tera Contributor

Hi Guys

 

Would this script work for a condition based on the last working day of the month?  running monthly.

 

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;


}

}

var now = new GlideDateTime();
var lwd = isLastWorkingDay(now);
answer = lw

 

 

Also, see highlighted in yellow

find_real_file.png

1 ACCEPTED SOLUTION

var gdt1 = new GlideDateTime();

if(gdt1.getDayOfMonth() >= 11){

var gdt = new GlideDateTime();

gs.log('Rodz triggered script');


var today = gdt.getDayOfWeekLocalTime();
var thisMonth = gdt.getMonthLocalTime();

 

if(today == 5){

var nextMonday = gdt;
nextMonday.addDaysLocalTime(3);
var nextMondayMonth = nextMonday.getMonthLocalTime();


if(nextMondayMonth != thisMonth){

 

 

}

View solution in original post

6 REPLIES 6

Jake Sadler
Kilo Sage

Hi,

 

You should run this script daily as you will need to check for the last working day on the 28th, 29th, 30th, 31.

 

Adding an if condition to check the day of month will prevent this from running before the 28th.

 

For example;

 

var gdt1 = new GlideDateTime();

if(gdt1.getDayOfMonth() >= 28){

insert your script here

}

Hi 

 

i made some modifications on a script ,

expecting it to run on friday, but it didn't here is the code

 

var gdt1 = new GlideDateTime();

if(gdt1.getDayOfMonth() >= 11){
function isLastWorkingDay(gdt) {

gs.log('Rodz triggered script');


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

}
var now = new GlideDateTime();
var lwd = isLastWorkingDay(now);
answer = lwd;
answer;
}

Hi,

 

You've passed gdt as  a parameter in the function but you haven't declared it.

 

Put var gdt = new GlideDateTime(); outside of the function.

 

Also you need to call the function. So above the function put isLastWorkingDay(gdt);

 

Thanks

 

Jake

Hi

 

I'm still a bit confused as to how you want me to structure these