Scheduled Report - send only on Monday-Wednesday-Friday - script not working

Baggies
Kilo Guru

Hello. I am trying to schedule a report to be sent every  Monday-Wednesday-Friday. I have pawed through numerous posts, and come up with same answer each time , BUT my report gets sent no matter what day it is.

If anyone has any ideas, I'd really appreciate them. Seems such an easy thing, yet I've just spent an hour wondering what's up. Cheers.

function checkWeekday(){
var today = new GlideDateTime();
var dayOfWeek = today.getDayOfWeek();
var answer = false;
if(dayOfWeek == 1 || dayOfWeek == 3 || dayOfWeek == 5){ //  0=Sunday,1=Monday,2=Tuesday,3=Wednesday,4=Thursday,5=Friday,6=Saturday
answer = true;
}
return answer;
}
checkWeekday();

 

find_real_file.png

 

1 ACCEPTED SOLUTION

Baggies
Kilo Guru

Workaround

To use date/time methods in your conditional script, for example, to send the Scheduled Report on specific days, you can use the JavaScript Date class instead of the GlideDate class. Using the JavaScript Date class, the following conditional script will send the report every weekday -

Note: The numbers for the days of the week in the JavaScript Date class are dependent on your timezone, so this will need to be confirmed on your own instance.

// This condition should cause the scheduled report to only run on a weekday (Mon - Fri)
// 0=Sunday,1=Monday,2=Tuesday,3=Wednesday,4=Thursday,5=Friday,6=Saturday
var d = new Date();
dayOfWeek = d.getDay();
if(dayOfWeek > 0 && dayOfWeek < 6) {
answer = true;
}
else {
answer = false;
}


The following conditional script will send the report on TuesdayWednesday and Friday, as follows -

// This condition should cause the scheduled report to only run on a Tue, Wed and Fri)
// 0=Sunday,1=Monday,2=Tuesday,3=Wednesday,4=Thursday,5=Friday,6=Saturday
var d = new Date();
dayOfWeek = d.getDay();
if(dayOfWeek == 2 || dayOfWeek == 3 || dayOfWeek == 5) {
answer = true;
}
else {
answer = false;
}

If the conditional script on a Scheduled Report is more complex and you need to make use of our Glide classes, then please use the following steps as a workaround -

1. Create a Scheduled Job and complete all the conditional logic of the Scheduled Report inside the Schedule Job.

2. If all the conditional logic is satisfied, you can trigger the Scheduled Report with the following script inside the Scheduled Job.

var schRpGr = new GlideRecord("sysauto_report");
schRpGr.get("<sys_id of the scheduled report>");
gs.executeNow(schRpGr);

View solution in original post

7 REPLIES 7

Baggies
Kilo Guru

Did find this though..

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0827366

Baggies
Kilo Guru

Workaround

To use date/time methods in your conditional script, for example, to send the Scheduled Report on specific days, you can use the JavaScript Date class instead of the GlideDate class. Using the JavaScript Date class, the following conditional script will send the report every weekday -

Note: The numbers for the days of the week in the JavaScript Date class are dependent on your timezone, so this will need to be confirmed on your own instance.

// This condition should cause the scheduled report to only run on a weekday (Mon - Fri)
// 0=Sunday,1=Monday,2=Tuesday,3=Wednesday,4=Thursday,5=Friday,6=Saturday
var d = new Date();
dayOfWeek = d.getDay();
if(dayOfWeek > 0 && dayOfWeek < 6) {
answer = true;
}
else {
answer = false;
}


The following conditional script will send the report on TuesdayWednesday and Friday, as follows -

// This condition should cause the scheduled report to only run on a Tue, Wed and Fri)
// 0=Sunday,1=Monday,2=Tuesday,3=Wednesday,4=Thursday,5=Friday,6=Saturday
var d = new Date();
dayOfWeek = d.getDay();
if(dayOfWeek == 2 || dayOfWeek == 3 || dayOfWeek == 5) {
answer = true;
}
else {
answer = false;
}

If the conditional script on a Scheduled Report is more complex and you need to make use of our Glide classes, then please use the following steps as a workaround -

1. Create a Scheduled Job and complete all the conditional logic of the Scheduled Report inside the Schedule Job.

2. If all the conditional logic is satisfied, you can trigger the Scheduled Report with the following script inside the Scheduled Job.

var schRpGr = new GlideRecord("sysauto_report");
schRpGr.get("<sys_id of the scheduled report>");
gs.executeNow(schRpGr);

atul_05
Tera Contributor

Just create 3 Scheduled Report instead of just 1.

You can then select Weekly and Day = Monday, for 2nd Scheduled Report Weekly and Day = Wednesday and so on..

atul_05_0-1696429356980.png

 

Thanks,

Atul