The CreatorCon Call for Content is officially open! Get started here.

Custom Discovery Schedule with a Condition Script

Jim Coyne
Kilo Patron

The "Run" choices we have when setting up a Discovery Schedule are:

  • Daily
  • Weekly
  • Monthly
  • Periodically
  • Once
  • On Demand

 

We would have to create multiple schedules in order to run a Discovery every weekday or just weekends.   Or do we? 

It may not be obvious, but because the Discovery Schedule table is a subclass of the Scheduled Job table, we have access to the Conditional checkbox and Condition script fields.   By adding those fields to the Discovery Schedule form, we can now use a script to decide when it should run.   So for example, set the "Run" field to "Daily", check the "Conditional" field and add the following script to the "Condition" field to run a Discovery on weekends only:

(function() {
  //run on weekends only
  var day = new Date().getDay();
  return (day == 0 || day == 6);

})();

 

Skip Sundays:

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

})();

 

This script for Monday to Friday:

(function() {
  //run Monday to Friday
  var day = new Date().getDay();
  return (day != 0 && day != 6);

})();

 

This one for Monday, Wednesday and Fridays:

(function() {
  //run on Monday, Wednesday and Fridays
  var day = new Date().getDay();
  return (day == 1 || day == 3 || day == 5);

})();

So what happens is the Discovery Schedule will in fact "run" every day at the specified time, but if the condition fails, the job ends without actually kicking off a Discovery.

1 ACCEPTED SOLUTION

Jim Coyne
Kilo Patron

Just setting this answer as correct as the actual thread was not meant as a question and it cannot be changed because it came from the original Community site.

View solution in original post

26 REPLIES 26

aleck_lin
ServiceNow Employee
ServiceNow Employee

Yah, that's a fair question. To truly solve the scheduling problem, there really needs to be another layer of scheduling capabilities built on top of sys_trigger and sysauto (by the way, I'm no longer in dev and this is my personal opinion). We solved the problem for Discovery because we introduced our own layer of complexity to work with those scheduled jobs. At the end of the day, I agree that this would be better solved on the platform side, but short of that, we at least made it possible for Discovery. We need folks like you to enter in enhancement requests to the platform team to make this possible.


KCallaghan
Mega Contributor

Excellent. I was looking for something just like this!


Malaya
Giga Expert

Could you assist with script for quarterly report?


Sure.   Set the "Run" field to "Monthly" and select the day you want it to run on (e.g. 1 for the first day of the month), check the "Conditional" checkbox and use the following script in the "Condition" field:



(function(){


      //run after each quarterly end date


      var month = new GlideDateTime().getMonthLocalTime();


      return (month == 1 || month == 4 || month == 7 || month == 10);


})();



That will then just run once a month and assumes calendar quarters.   The report would then run on the first day of January, April, July and October, the day after the quarterly end dates.   You might have to tweak for your own needs.


Thanks Jim.