Run the scheduled job only on weekdays except Sat and Sun

SNOW46
Tera Contributor

Hello All,

I have a requirement that when Incident Status is changed to On Hold, then On Hold Reason will open were Assigned to User needs to select as Enhancement option from the drop-down menu and the SLA will get Paused.

Once the user selects the Enhancement option, then a new Date/Time field will open below the On hold Reason field.

When the Assigned to user selects any particular Date and Time so till that duration or date/time the Incident will remain as On Hold and once the duration is completed or the Date is crossed, then the Status should automatically be changed to In Progress and at the same time a notification should trigger to the Assigned to User stating the Incident Status has been changed to In Progress now.

We have defined a Scheduled Job script for the same but it should not run on Sat and Sun as we have the SLAs running for different teams. I have tested some of the Incidents where I found that the scheduled job has run on weekends and the SLA stage changed from Pause to In Progress.

find_real_file.png

find_real_file.png

 

//var now = new GlideDateTime();
var inc = new GlideRecord('incident');
inc.addEncodedQuery('u_hold_reason=9^u_conditional_dateONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()^ORu_conditional_date<=javascript:gs.endOfToday()');
inc.query();
// gs.log('xyz : Enhancement');
while (inc.next()) {
     gs.eventQueue('conditionalholdtime',inc,'','');  
    gs.log('xyz: past or now time');
        inc.state = '2';
        inc.update();
}

 

Can someone help me on this to make this not run on weekends that are on Sat and Sunday to make sure that SLA is not getting to In Progress as I have a call with customer for the demo on Wednesday?

 

Thanks

12 REPLIES 12

I just ran a background script to check, and the values are correct in that Saturday = 6 and Sunday = 7.

So the script should run as expected and stop the scheduled job from running on a Saturday or Sunday.

 

You can check for yourself on your instance by adding this to the "Scripts - Background" window and clicking the Run Script button:

var gdt = new GlideDateTime("2022-06-26 12:00:00");
gs.print(gdt.getDayOfWeek());

Then you can change the date value to any other date to check/confirm the day values.

snowycode
Tera Contributor

Hey @SNOW46 - we had a similar issue and ended up using the following script to work. It can include/exclude any days you need by using the 0-6 number assigned to each day.

 

// get the date in 'Thu Dec 08 2022 07:00:53 GMT-0800 (PST)' format
var d = new Date();
// convert the date to a number between 0-6 (0 is Sunday, 6 is Saturday)
var day = d.getDay();

// if the day is between 1 and 5 (Monday - Friday), then return true 
if ( day > 0 && day < 6 ) {
	return true;
} else {
	return false;
}

 

 

For an explanation on the code, you can see how we excluded weekends from scheduled jobs here.

 

pbusch
Tera Expert

This conditional script works, running Washington DC

 

// Run the scheduled report only on weekdays (Non - Fri)
// 0=Sunday,
// Get today's date and determine which day it Is
var today = new Date();
dayOfWeek = today. getDay();
// It it is Monday, Tuesday, Wednesday, Thursday, or Friday, send report
// If it is Saturday or Sunday, do not send the report.
if (dayOfWeek > 0 && dayOfWeek < 6){
answer = true;
}
else {
answer = false;
}
 
from this post:
Enjoy!
~ "Breynia Disticha"