Scheduled Report's Condition script is not working properly

Ajay37
Tera Contributor

Hi,

I need my report to run only on weekdays. For this I am using the below script. The issue is, it has run even on Saturday, and didn't trigger on Sunday and Monday.

Whatever the number my instance is updating for the day, the below condition gets satisfied right?

For the below number combination:

0=Sunday, 1=Monday, 2=Tuesday, 3=Wednesday, 4=Thursday, 5=Friday, 6=Saturday 

As I have given dayOfweek > 0, it should skip Sunday and for dayOfWeek<6 it should skip Saturday. 

If we consider below number combination also, the above condition will get satisfied right. I am not getting what the issue is.

1=Monday, 2=Tuesday, 3=Wednesday, 4=Thursday, 5=Friday, 6=Saturday, 7= Sunday

 

var now = new Date();
dayOfWeek = now.getDay();

if (dayOfWeek > 0 && dayOfWeek < 6) {
    answer = true;
} 
else {
    answer = false;
}

Thanks

4 REPLIES 4

Ajay37
Tera Contributor

Hi @Ankur Bawiskar ,

Could you please help on this?

Thanks

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

update as this

var answer = false;

//Get the day of week. 1=Monday, 7=Sunday
var now = new GlideDateTime();

//Run only on weekdays
if(now.getDayOfWeek() < 6){
   answer = true;
}
answer;

Regards
Ankur

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

Hi @Ankur Bawiskar ,

Thank you. But GlideDateTime() is not working on scheduled reports, so using new Date().

Hi,

okay got it

try this and add logs as well

var nowDate = new Date();
dayOfWeek = nowDate.getDay();

dayOfWeek = parseInt(dayOfWeek);

if (dayOfWeek > 0 && dayOfWeek < 6) {
	answer = true;
} 
else {
	answer = false;
}

Regards
Ankur

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