Use PDIs? Take our 5-minute survey to help shape the PDI roadmap.

Australia upgrade broke script logic in Scheduled Jobs

ChrisWing
Giga Guru

We had a need to run certain scheduled jobs on a specific schedule that wasn't supported out of the box.  To that end we came up with a simple script to meet our requirements.

var run = false;
var today = new Date();
var weekday = today.getDay();

if (weekday > 0 && weekday < 6)
{
run = true;
}

else

{
run = false;
}

Once we upgraded to Australia, this is no longer working for us.  I have opened a case with ServiceNow support regarding this, and they came back and said the Run cannot be executed because it has no meaning outside of the script however it worked in Yokohama.  Anyone else encountered this, or have a solution?  They are pointing me to a KB article about running Flows on a specific day of the week, but what I was doing existed only in scheduled jobs and had no flows involved.  Am I going to have to build a flow now to make this work?

6 REPLIES 6

Ankur Bawiskar
Tera Patron

@ChrisWing 

did you try setting answer variable to true/false?

answer = checkCondition();

function checkCondition(){
var run = false;
var today = new Date();
var weekday = today.getDay();
if (weekday > 0 && weekday < 6)
{
run = true;
}
else
{
run = false;
}
return run;
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

drbob
Tera Guru

Just to address unnecessary code/logic - you don't need this...

 

else
{
run = false;
}

 

...you already defaulted it to false in the first line of the function so you only need the "if" to set the true case.


By observation, all the OOTB scripts use the self-invoking function structure. To be equivalent in your original script you probably should have this at the top:

 

answer = checkCondition();

return answer;

The platform is inconsistent, some places you just need to set the appropriate variable with the value, others you have to return it.

 

 

 

...but those two points aside, when you tried the code suggested by Harish you say it still doesn't work ?

 

ServiceNow now supports later versions of JavaScript - it may be that the change to do that has broken your script (surfaced an error by being more strict when previously it was sloppily letting it by, maybe). ...but the self-invoking should be okay.

 

Could be some weird TZ thing - maybe you are running it at the weekend but close to the time change so the code actually runs on a "different day" ? Other than that, there's nothing obvious. Suggest you put some logs in there to see what "run" (your code) and "answer" (Harish's code) is set to as the code exits.