The CreatorCon Call for Content is officially open! Get started here.

How to get second tuesday of every month for scheduling the report in servicenow

Chaitanya Redd1
Tera Guru

Hello Team,

Can anyone help me on the below script. I have to create schedule a report for every month second Tuesday and second Wednesday but it's not working can anyone help me.

var prefixes = ['First', 'Second', 'Third', 'Fourth', 'Fifth'];
    var result;

var gdt = '2019-12-10 00:00:19';
gs.print(gdt);
 
    var month = gdt.getMonthLocalTime();
    var day = gdt.getDayOfWeek();
    var week = gdt.getDayOfMonthLocalTime();
 
    var checkWeek = prefixes[0 | week / 7];
 
   if (day == 2 && (gdt.getDate() >7 && gdt.getDate()<=14)) {
        result = true;
    } else {
        result = false;
    }
    gs.print(result);
1 ACCEPTED SOLUTION

asifnoor
Kilo Patron

Hi,

Try this code. here are the changes made and its working in bg script.

1. the date/time has to be converted to glidedatetime

2. getDate() returns complete date not days. so corrected that.

var dt = '2019-12-10 00:00:19';
var gdt = new GlideDateTime(dt);
gs.print(gdt);
var day = gdt.getDayOfWeek();
var days = gdt.getDayOfMonthLocalTime();
gs.print(day); 
gs.print(days); 
   if (day == 2 && ((days > 7  && days <=14) || (days >21 && days <=28))) {
        result = true;
    } else {
        result = false;
    }
gs.print(result);

Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.

Regards,
Asif
2020 ServiceNow Community MVP

View solution in original post

15 REPLIES 15

it's also getting false

*** Script: 2020-04-14 07:52:24
*** Script: false

Thats because today is not 14th of april 🙂

Your result should only be true when today is the 2nd tuesday in the month

If you change "today" variable with 14th of april then it returns true

var today = new GlideDateTime("2020-04-14");

Can you help me for 1st Monday/Tuesday/ Wednesday as well take example for this month 1st of April is Wednesday and 6th April is First Monday and 7th April is first Tuesday. then how to schedule reports for all this three days First Monday/Tuesday/ Wednesday.

Hi again

Is it the same schedule that should run or do you have specific schedules for each day?

Different Scheduled Reports for different Days