How do I schedule a sys_template to trigger on the 2nd Monday of every month?

JR42
Giga Guru

I need to trigger a template which generates an Incident on the 2nd Monday of every month.

I have tried a number of scripts but they have not worked.  They either don't trigger, trigger every day, or trigger every Monday.  Does anyone have a working script for this?  Below is my latest attempt, which is triggering every day.

 

Justin3_0-1715095091229.png

 

//months - list the months to run

//week is a number 1-5 representing week number

//dayOfWeek is 0 = Sunday, 6 = Saturday

**var** runSchedule = **function** (months, week, dayOfWeek) {

**if**( week < 0 || week > 5 ) {

**return** false;

}

**if**( dayOfWeek < 0 || dayOfWeek > 6 ) {

**return** false;

}

**var** today = **new** Date();

**var** month = today.getMonth() + 1;

**if**( months.indexOf(month) == -1 ) {

**return** false;

}

**var** td = **new** Date(today.getTime());

**while**( td.getDate() != 1 ) {

td = **new** Date(td.getTime() - 86400000);

}

**while**( td.getDay() != dayOfWeek ) {

td = **new** Date(td.getTime() + 86400000);

}

**var** firstDay = td.getDate();

**return** (firstDay + ((week-1)*7)) == today.getDate();

};

//runSchedule([months], week, dayOfWeek);

runSchedule([1,2,3,4,5,6,7,8,9,10,11,12], 2, 1);

 

 

Thanks!

1 ACCEPTED SOLUTION

Kieran Anson
Kilo Patron

Hi Justin,

Best to use GlideDateTime when processing dates server side in ServiceNow, the below function can be used to check the date and whether it's the 2nd Monday. Set your scheduled job to run daily, and use this function within the script

 

function is2ndMonday (gdt){
  var dayOfWeek = gdt.getDayOfWeekUTC();
  var dayOfMonth = gdt.getDayOfMonthUTC();
  var MONDAY = 1;
  
  //Not a monday, no point checking further  
  if(MONDAY !== dayOfWeek)
    return false;
  
  //We're either at a week or below, so can't 
  //of had two mondays
  if(dayOfMonth < 8)
    return false;
  
  //We're above 14 days, so can't be the second monday
  if(dayOfMonth > 14)
    return false;
  
  return true;
  
}

is2ndMonday(new GlideDateTime());

View solution in original post

4 REPLIES 4

Kieran Anson
Kilo Patron

Hi Justin,

Best to use GlideDateTime when processing dates server side in ServiceNow, the below function can be used to check the date and whether it's the 2nd Monday. Set your scheduled job to run daily, and use this function within the script

 

function is2ndMonday (gdt){
  var dayOfWeek = gdt.getDayOfWeekUTC();
  var dayOfMonth = gdt.getDayOfMonthUTC();
  var MONDAY = 1;
  
  //Not a monday, no point checking further  
  if(MONDAY !== dayOfWeek)
    return false;
  
  //We're either at a week or below, so can't 
  //of had two mondays
  if(dayOfMonth < 8)
    return false;
  
  //We're above 14 days, so can't be the second monday
  if(dayOfMonth > 14)
    return false;
  
  return true;
  
}

is2ndMonday(new GlideDateTime());

If you want to validate this works, you can run the following in a background script to get the 2nd monday for as many days as you want to check

 

function is2ndMonday (gdt){
  var dayOfWeek = gdt.getDayOfWeekUTC();
  var dayOfMonth = gdt.getDayOfMonthUTC();
  var MONDAY = 1;
  
  //Not a monday, no point checking further  
  if(MONDAY !== dayOfWeek)
    return false;
  
  //We're either at a week or below, so can't 
  //of had two mondays
  if(dayOfMonth < 8)
    return false;
  
  //We're above 14 days, so can't be the second monday
  if(dayOfMonth > 14)
    return false;
  
  return true;
  
}

var today = new GlideDateTime();
var count  = 50; //Days forward to check

do {
  var is2nd = is2ndMonday(today);
  if(is2nd)
  	gs.info(today.getDate().getDisplayValue() + " - " + is2nd);
  today.addDays(1);
  count --
  
  
  
  
} while (count > 0 );

KieranAnson_0-1715099200940.png

 

Thanks Kieran.  The ticket successfully triggered on the 2nd Monday.  I am just waiting to validate that it does not trigger next Monday, then I'll accept your answer and add all the helpfuls.

 

Do you have any tips or links to good info on how I can use the background script module to test and validate my scripts, such as the one I posted originally?

I'm personally not a fan of background scripts (although the new "modern" background scripts in Washington DC is a lot nicer)

 

I, and a cohort of other developers, use Xplore. It's a much nicer user interface for script development and testing.

 

Xplore: Developer Toolkit for ServiceNow (xploretoolkit.com)