How to schedule a report to run at 3 times in a day (9 , 12 and 16) from Monday to Friday only

tejapolaki
Kilo Contributor

We have requirement to schedule report to run at different time intervals in a day ( not uniform) during weekdays , is there a way it can be done to schedule report with the above requirements.

Thanks.

13 REPLIES 13

Try this:

Edit: Some updated code

answer = ifScript();
function ifScript(){
	var d = new GlideDateTime(); //setup date time object
	d.setValue(gs.nowDateTime()); //set to current date/time
	var dt = d.getTime().toString().split(" ")[1];
	var time = dt.split(":");   //Gets Hours
	var currenTime = time[0] + ":" + time[1];
	if(d.getDayOfWeekUTC() != 6 && d.getDayOfWeekUTC() != 7){ //Keep from running on Saturday and Sunday.
		if (currenTime == "04:30" || currenTime == "12:45"){ // Run at 4:30 am and 12:45 PM. If this needs to be 12 am then change to 00:45
			return true;
		}
		else{
			return false;
		}
	}
	else{
		return false;
	}
}

Hi Brian, great solution here!  If I may, there are more concise ways to get the data you're looking for with GlideDateTime():

answer = ifScript();
function ifScript() {
    var gdt = new GlideDateTime();
    var currentTime = gdt.getLocalTime().getByFormat('HH:mm');
    if (gdt.getDayOfWeekLocalTime() != 6 && gdt.getDayOfWeekLocalTime() != 7) { //Keep from running on Saturday and Sunday.
        if (currentTime == "04:30" || currentTime == "12:45") { // Run at 4:30 am and 12:45 PM. If this needs to be 12 am then change to 00:45
            return true;
        }
    }
    return false;
}

GlideDateTime() automatically instantiates with the current time but interestingly, your method of setting the time works because you're setting the GDT to local time and the rest of your code works with that assumption in mind.  The only downside is that that method can lead to ambiguity since you're omitting time zone which would render calls to get local date/time or getDisplayValue inaccurate.   For example, .getDayOfWeekUTC() is not actually returning UTC, it is returning local date.

I only post this because I have had nightmares trying to resolve date/time issues.  Things that work well for a while, but as soon as DST rolls around, times start calculating incorrectly (I don't think this will happen with your code though). Or cases where I'm picking up someone else's script and trying to compare or calculate one GDT to another, not realizing that one is set in local time while the other is set in UTC.

Hope I'm not overstepping here and that you find this helpful.

Jon G1
Kilo Sage

I know this is old, but since it came up at the top of my search in google, I thought I'd post a note here for anyone who might come across it in the future.  I built out something a bit more robust based on the code @Alikutty Abdulrazak posted above.

I posted an article here if you want more info. I've posted the script below if you just want to get to it. You can easily set which days of the week and which hours you want the script to run in the arrays at the top.

//Use Run: Periodically, Interval: 1 hour (check conditions every hour - send if appropriate)
(function () {

	var runHours = [9, 14, 16]; //Hour of day to send (24 hour time)
	var runWeekdays = [1, 2, 3, 4, 5]; //Days of week - (Mon = 1, Tues = 2... Sun = 7)
	var answer = false; //Default to false

	var shouldRunToday = checkRunWeekday(runWeekdays);
	var shouldRunThisHour = checkRunHour(runHours);

	if (shouldRunToday && shouldRunThisHour) answer = true;

	return answer;

})();

function checkRunWeekday(runDays) {
	//Return true if we should send today. Otherwise, return false
	var currentDayOfWeek = getCurrentDayOfWeek();
	if (runDays.indexOf(currentDayOfWeek) > -1) return true;
	return false;
}

function getCurrentDayOfWeek() {
	var gdt = new GlideDateTime();
	var dayOfWeek = gdt.getDayOfWeekLocalTime();
	return dayOfWeek;
}

function checkRunHour(runHours) {
	//Return true if we should send this hour. Otherwise, return false
	var hour = getCurrentHour();
	if (runHours.indexOf(hour) > -1) return true;
	return false;
}

function getCurrentHour() {
	var gdt = new GlideDateTime();
	var time = gdt.getLocalTime().toString().split(" ")[1];
	var hour = parseInt(time.split(":")[0]);
	return hour;
}

gyedwab
Mega Guru

If you're looking to do this frequently, you can do this codelessly with Explore Analytics -- our scheduler allows you to only distribute on certain days of the week. You could define one scheduled report/dashboard for each time of day.