How to make a scheduled job not run on the first week of a month and not run on FEB and AUG

Joshua Comeau
Kilo Sage

first week of feb and aug have it run
but if its any other month do not run on the first week

 

Tried this but not working

 

//start

function checkMonth()

{

var gd = new GlideDateTime();

var getWeek = gd.getWeek();

var getMonth = gd.getMonth();

 

//Update weeks as per your requirement

 

if(getWeek == 1 && getMonth == 2 && getMonth == 8) {

return false;

}

 

else{

return true;

}}

 

//end

2 REPLIES 2

Bert_c1
Kilo Patron

your use of gd.getWeek() seems wrong, you need to change your logic. maybe check gs.getDayOfMonth() described here: https://developer.servicenow.com/dev.do#!/reference/api/washingtondc/server_legacy/c_GlideDateTimeAP...

 

 

 

AnveshKumar M
Tera Sage
Tera Sage

Hi @Joshua Comeau 

You can use the following script to check for the week, but what do you mean by first week? Is it first 7 days or the first real week (day on or before first Sunday if the first day of month not starts with a Sunday, if the first day of month starts with Sunday, then the days before second sunday)?

 

If it is just first 7 days of month use this script:

 

function checkMonth(){

var gd = new GlideDateTime();

var getMonth = gd.getMonthLocalTime();

var dayOfMonth = gd.getDayOfMonthLocalTime();

var isInFirstWeek = dayOfMonth <= 7;

 

//Update weeks as per your requirement

if(isInFirstWeek && getMonth == 2 && getMonth == 😎{

return false;

}else{

return true;

}

}

 

If you are checking for first calendar week of month, use this:

 

function checkMonth(){

var gd = new GlideDateTime();

var getMonth = gd.getMonth();

 

var dayOfMonth = gd.getDayOfMonthLocalTime();

if(dayOfMonth != 1){

   //get First day of month

   gd.addDaysLocalTime((-1 * (dayOfMonth - 1)));

}

 

while(gd.getDayOfWeekLocalTime() != 7){

gd.addDaysLocalTime(1);

}

var firstSunday = gd.getDayOfMonthLocalTime();

 

var isInFirstWeek = dayOfMonth <= firstSunday;

 

//Update weeks as per your requirement

if(isInFirstWeek && getMonth == 2 && getMonth == 😎 {

return false;

}else{

return true;

}

}

 

 

If your criteria is to check if the month starts with Sunday then you need to check till the second sunday, use the one below:

 

 

If you are checking for first calendar week of month, use this:

 

function checkMonth(){

var gd = new GlideDateTime();

var getMonth = gd.getMonth();

 

var dayOfMonth = gd.getDayOfMonthLocalTime();

if(dayOfMonth != 1){

   //get First day of month

   gd.addDaysLocalTime((-1 * (dayOfMonth - 1)));

}

if( gd.getDayOfWeekLocalTime() == 7){

gd.addDaysLocalTime(1);

}

while(gd.getDayOfWeekLocalTime() != 7){

gd.addDaysLocalTime(1);

}

var firstSunday = gd.getDayOfMonthLocalTime();

 

var isInFirstWeek = dayOfMonth <= firstSunday;

 

//Update weeks as per your requirement

if(isInFirstWeek && getMonth == 2 && getMonth == 😎 {

return false;

}else{

return true;

}

}

 

 

Please mark my answer helpful 👍 and accept as a solution if it helped.

Thanks,
Anvesh