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

Try this condition script:

answer = (function(){
    //initialize
    var result = false;
    var tuesdays = [];
    var today = new Date().getDate();
    var day = new Date();
    var month = day.getMonth();

    //start on the first day of the month...
    day.setDate(1);

    //...and find the first Tuesday in the month
    while (day.getDay() !== 2) {
        day.setDate(day.getDate() + 1);
    }

    //get all the other Tuesdays in the month
    while (day.getMonth() === month) {
        tuesdays.push(day.getDate());
        day.setDate(day.getDate() + 7);
    }
 
    //is today the 2nd or 4th Tuesday? (array index starts at 0)
    if (today == tuesdays[1] || today == tuesdays[3]) {
        result = true;
    }
    return result;
})();

Thanks Jim!  Ill give it a shot.  Should I set the Run frequency to weekly or monthly.  I'm thinking I will have to set it to weekly on 2nd Tuesday and the conditional script will handle the rest?

 

Thanks for the help!

 

Russell

You would have to set "Run" to "Weekly" and "Day" to "Tuesday":

find_real_file.png

It would "trigger" every Tuesday, but the Condition script would really only allow it to execute on the 2nd and 4th Tuesdays.

Just in time to test it out tomorrow  🙂

Russell Park
Mega Guru

Jim,  I check this morning and no luck on the scheduled job creating the ticket.  Take a look at the attachment and let me know if you see something wrong with the code.  When I manually execute the job it does create a ticket. Thanks for the help.  Russell

 

I wonder if timezones are coming into play?  That may account for it running if you manually trigger it but did not this morning.