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

Script not working in scheduled reports

Sireesha7
Tera Contributor

Hi Team,

 

Good Day !

 

I wrote a script in Scheduled report and it's not working and when I run same script in background script it's working fine.

 

The scenario is a scheduled report that is supposed to run on the second Monday of each quarter . However, it is incorrectly triggering on the first Monday of every week. 

 

I am attaching the script and background script executions as well.

Please check and do the needful !

 

image (9).pngimage (10).png

 

 

Thanks & Regards,

Sai Sireesha G

 

20 REPLIES 20

@Ankur Bawiskar 

 Sorry No Luck, Still its not working

 

@Sireesha7 

I believe logs don't come from condition script field in scheduled report

But this logic you can use to check if it's 2nd monday of each quarter using native Date() object

(function() {
  var answer = false;
  var now = new Date(); // Current date/time

  var month = now.getMonth(); // 0=Jan, 1=Feb, ..., 11=Dec
  var dayOfWeek = now.getDay(); // 0=Sunday, 1=Monday, ..., 6=Saturday
  var date = now.getDate(); // 1-31

  // Check if this is the first month of a quarter
  if (month === 0 || month === 3 || month === 6 || month === 9) {
    // Check if today is Monday
    if (dayOfWeek === 1) {
      // Check if this is the 2nd Monday (date between 8 and 14)
      if (date > 7 && date <= 14) {
        answer = true;
      }
    }
  }

  // For ServiceNow Scheduled Reports, the script must return true/false
  return answer;
})();

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar ,

 

As you suggested, I have updated the same, As per the condition we shouldn't receive report but still we are receiving it.

I have attached the screenshot as well.image (13).png

 

Please have a look and let me know your feedback 

 

Regards,

Sireesha G

@Sireesha7 

it worked fine for me

14th July 2025 is 2nd Monday of 3rd quarter, so it returned true for me

Remember when you use Date() object the months start from 0, 1, 2

0 sunday, 1 monday .....

(function() {
  var answer = false;
  var now = new Date(2025, 6, 14); // Example: July 14, 2025 (second Monday of Q3)

  var month = now.getMonth(); // 0=Jan, 1=Feb, ..., 11=Dec
  var dayOfWeek = now.getDay(); // 0=Sunday, 1=Monday, ..., 6=Saturday
  var date = now.getDate(); // 1-31

  // Check if this is the first month of a quarter (Jan, Apr, Jul, Oct)
  if (month === 0 || month === 3 || month === 6 || month === 9) {
    // Check if today is Monday and the 2nd Monday (date between 8 and 14)
    if (dayOfWeek === 1 && date >= 8 && date <= 14) {
      answer = true;
    }
  }

  gs.info('Is second Monday of a quarter? ' + answer);
})();

AnkurBawiskar_1-1749200730857.png

 

AnkurBawiskar_0-1749200716260.png

 

Try passing different values and see and then add that code in scheduled report, it should work

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Sireesha7 ,

is it a scoped add the scheduled job is in?

 
gdt.getMonthUTC()

 intead of geMonth

 
gd.getDayOfMonthUTC()

 intead of getDayOfMonth

 
 
or you can go with script 
by changing the run to weekly on mondays
var gdt = new GlideDate();
var day = gdt.getDayOfMonthNoTZ()
var month = gdt.getMonthNoTZ()

answer = (day > 7 && day < 14) && (month == 1 || month == 4 || month == 7 || month == 10);

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya