Scheduled Report - not working with condition

oharel
Kilo Sage

Hi all,

I would like to send a report every hour between 15:00 and 20:00, every day except for Sunday.

The scheduled report is configured like so:

find_real_file.png

I have the following condition in a scheduled report (based on @Brian Lancaster 's answer here; thanks Brian!)

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(":")[0];   //Gets Hours
	
	//Sunday (7), Monday (1), Tuesday (2), Wednesday (3), Thursday (4), Firday (5), Saturday (6)
	if(d.getDayOfWeekUTC() != 7){ //Don't run on Sunday.
		if(time == "15" || time == "16" || time == "17" || time == "18" || time == "19"){ //run at 15,16,17,18,19
			return true;
		}
		else{
			return false;
		}
	}
}

Still, the report is generated every hour, even before 15:00 and after 20:00.

Not sure why.

(Running the script in a background script shows the correct response...)

Your thoughts are welcome.

harel

1 ACCEPTED SOLUTION

oharel
Kilo Sage

After contacitng Hi, this is the final script that works:

//Sunday (0), Monday (1), Tuesday (2), Wednesday (3), Thursday (4), Friday (5), Saturday (6)

var answer = false;

var now = new Date();
var hours = now.getHours() + 10;
if(now.getDay() != 0 && hours >= 15 && hours <= 20) {
	answer = true;
}
gs.log('hbs --> now ' + now.getDay() + ' time ' + hours + ' answer ' + answer);
answer;

Also, to get the log statement logged: gs.log() is ignored silently in sandbox. You can edit system property "glide.security.sandbox_no_logging" to set it to false to enable logging.

harel

View solution in original post

10 REPLIES 10

Brian Lancaster
Tera Sage

I don't see anything wrong with your script.  However you have it set to run periodic ever 15 hours.  I think you need to set it to run every 1 hour starting at 15:00:00.  Another strange thing I noticed from your screenshot is the field time.  That does not show for me when I select periodic.  

find_real_file.png

True, that. Something is wrong with my Orlando instance. I checked the UI policies and client scripts, tried to "watch" the field - nothing comes up. In London, it does not show. Perhaps a bug with the newest version? 

Brian Lancaster
Tera Sage

Sorry looks like you do have it set to run ever hour however I'm not sure why that time field is showing.  It should not be there when you choose periodic.  I'm wondering if that is some who causing the issue.

Tony Chatfield1
Kilo Patron

Hi, testing the script in a background window results in undefined when the day statement is not met IE day == 7. Otherwise, as you indicated, it runs and returns a true\false result, although the Date\Time functions are a mix of local and UTC, so depending on which account runs the scheduled job\script (and it’s related TZ) there may be inconsistencies.

I dropped the code into a scheduled report in NewYork and while debugging shows it returning true, I did not see a report being generated.

Reviewing the developer site, there is an example

https://developer.servicenow.com/dev.do#!/learn/learning-plans/orlando/servicenow_administrator/app_...

and also it states
The last line of a condition script must evaluate to either true or false. (Not return true or false...evaluate to true or false).

Updating the script to align with this seems to work for me and also resolved the undefined day issue without adding an else clause to the function.

 Perhaps try something like this (still may need the local\UTC date\times reviewed)

function ifScript() {
    var answer = false;
    var d = new GlideDateTime(); //setup date time object
    d.setValue(gs.nowDateTime()); //set to current date/time
    var dt = d.getTime().toString().split(" ")[1];

    gs.info("report test myTime " + d);

    var time = dt.split(":")[0]; //Gets Hours

    //Sunday (7), Monday (1), Tuesday (2), Wednesday (3), Thursday (4), Firday (5), Saturday (6)
    if (d.getDayOfWeekUTC() != 7) { //Don't run on Sunday.
        if (time == "15" || time == "16" || time == "17" || time == "18" || time == "19") { //run at 15,16,17,18,19
            answer = true;
        }
    }
    gs.info("report test " + answer);
	return answer;
}

ifScript();