How is this Scheduled Job Condition code working correctly?

davidc1
Kilo Contributor

I encountered some code written by another person in a "Condition" in a Scheduled Job.  This Condition prevents the scheduled job from running on Sundays as expected.  However, I don't understand why it works.  The value of answer is only ever set once to false and the checkDay function that he wrote does not set the value of answer.  Any ideas why this is working?

var gdt = new GlideDateTime();
var dayOfWeek = gdt.getDayOfWeekUTC();
var answer = false;

function checkDay(day){ //The day of week value from 1 to 7. Monday=1, Sunday=7
	if (day >= 1 && day < 7){
		return true;
	}
	return false;
}

checkDay(dayOfWeek);
1 ACCEPTED SOLUTION

Jim Coyne
Kilo Patron

The Condition field is a little weird.  Here's the explanation from the "Schedule a script execution" article on the Docs site:

"The last expression of the script should evaluate to a Boolean (true/false) value."

So basically, the last line in the script will dictate the result.  The "answer" variable is not even considered when evaluating the condition in your posted code.

And here is a simplified script for the condition:

(function() {
   //skip Sundays
   var day = new Date().getDay();
   return (day != 6);

})();

It will return true if it is not Sunday.  I've updated my "Custom Discovery Schedule with a Condition Script" blog post with that sample.  I know it talks about Discovery Schedules, but it works for all Schedule Jobs and thought it might be helpful.

View solution in original post

3 REPLIES 3

Brian Bouchard
Mega Sage

Usually when you see something like this you set the value of answer to true or false, then return the variable answer.  In this case it looks like the variable answer isn't utilized at all and the code is returning the true or false answer directly.


Prateek kumar
Mega Sage

He is calling the function on the last line

Run this code in the background script you'll understand the result returned.

var gdt = new GlideDateTime();
var dayOfWeek = gdt.getDayOfWeekUTC();
var answer = false;
gs.info(answer);
function checkDay(day){ //The day of week value from 1 to 7. Monday=1, Sunday=7
if (day >= 1 && day < 7){
return gs.info('true');
}
return gs.info('false');
}

checkDay(dayOfWeek);


Please mark my response as correct and helpful if it helped solved your question.
-Thanks

Jim Coyne
Kilo Patron

The Condition field is a little weird.  Here's the explanation from the "Schedule a script execution" article on the Docs site:

"The last expression of the script should evaluate to a Boolean (true/false) value."

So basically, the last line in the script will dictate the result.  The "answer" variable is not even considered when evaluating the condition in your posted code.

And here is a simplified script for the condition:

(function() {
   //skip Sundays
   var day = new Date().getDay();
   return (day != 6);

})();

It will return true if it is not Sunday.  I've updated my "Custom Discovery Schedule with a Condition Script" blog post with that sample.  I know it talks about Discovery Schedules, but it works for all Schedule Jobs and thought it might be helpful.