Have a Scheduled Job to Create a Request only run Monday thru Friday

terrieb
Tera Guru

I created a scheduled job to create a request that I only want to generate on Monday thru Friday.

Below is the script I put into the advanced tab on the scheduled job record:

 

function checkCondition() {

    var gdt = new GlideDateTime();

    var dayOfWeek = gdt.getDayOfWeekLocalTime();

    // Monday is 1, Sunday is 7

    if (dayOfWeek >= 1 && dayOfWeek <= 5) {

        return true; // Run the job

    } else {

        return false; // Don't run the job

    }

}

 

However, it is run every day, including Saturday and Sunday.

 

Have I missed a step?

 

The job is scheduled to run Daily on the US/Eastern time zone schedule

 

Thanks!

1 ACCEPTED SOLUTION

Did you select the option to have the Flow variable as a boolean True/False ?

flow-variable-types.png

View solution in original post

9 REPLIES 9

OlaN
Giga Sage
Giga Sage

Hi,

Probably a small error, you'll need to call the function within the script also.

 

Like this:

 

checkCondition(); // add this line

function checkCondition() {

    var gdt = new GlideDateTime();
    var dayOfWeek = gdt.getDayOfWeekLocalTime();
    // Monday is 1, Sunday is 7
    if (dayOfWeek >= 1 && dayOfWeek <= 5) {
        return true; // Run the job
    } else {
        return false; // Don't run the job
    }
}

Thanks.

 

Will monitor and see if it creates it again on Saturday this week.  

 

If it doesn't, I will mark as Helpful and Accept as Solution.

 

Thanks!

So I added that script and now it is creating 2 requests daily...

 

Here is the full script:

 

checkCondition();

function checkCondition() {
    var gdt = new GlideDateTime();
    var dayOfWeek = gdt.getDayOfWeekLocalTime();
    // Monday is 1, Sunday is 7
    if (dayOfWeek >= 1 && dayOfWeek <= 5) {
        return true; // Run the job
    } else {
        return false; // Don't run the job
    }
}
var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var item = cart.addItem('ae71ad2b2b292e90c433fe0ac191bf21');
var rc = cart.placeOrder();
gs.addInfoMessage(rc.number);
 
Have any idea why?  It wasn't doing that until i added the checkCondition at the beginning!

Did you add the entire script to the condition field ?

So the conditionfield also runs the cart-API ?

 

Since it's kind of a simple logic you want to run in your scheduled job, I would suggest you replace it with a simple Flow instead.
Here's how I would do it.
A flow that runs daily, and first action is to evaluate if it is a weekday or weekend, and depending on the results, run the submit of the catalog item needed.

submit-catalog-item-daily.png

 

Script for evaluating the weekend expanded below.

set-weekend-variable-expanded.png