Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Exclude weekends and holidays from scheduled job query

dagarson
Tera Guru

Hello I have a scheduled job that is suppose to fire a notification reminder 3 days before the listed date on a field, the script currently works fine however I need to exclude weekends from the dates here is my script

 

var gr = new GlideRecord('sn_hr_core_task');

gr.addEncodedQuery('short_description=Test Record^active=true');
gr.addQuery('parent.ref_sn_hr_le_case.date', '=', gs.daysAgo(-3)); 
gr.query();

while(gr.next()){
 	gs.eventQueue("sn_hr_le.reminder",gr);
}

Any assistance would be appreciated 

3 REPLIES 3

Aman Kumar S
Kilo Patron

Hi @dagarson ,

 

The best way to approach this sort of problem is using Schedules, I am sure you would have heard of it, these are available out of the box as well as companies also create specific schedules as per their need, ex- holidays, workdays.

 

You can make use of these concepts to make your script more robust and precise.

var date = current.your_Date_field;
var schedule = new GlideSchedule('schedule_sys_id'); 
		if(schedule.isInSchedule(date)) {
			return true;
		} else {
			return false;
		}
Best Regards
Aman Kumar

Thank you I tried adding the schedule into my script but it doesnt seem to be working not sure where Im going wrong

 

var gr = new GlideRecord('sn_hr_core_task');

gr.addEncodedQuery('short_description=Test Record^active=true');

gr.query();

while(gr.next()){
		var gdt = current.parent.ref_sn_hr_le_case.date;
gdt.addDays(-3);

	var schedule = new GlideSchedule('838a2e7f1bdbbc10dffb859ce54bcbc0');
	
	if(!(schedule.isInSchedule(gdt)))
	{
		gs.eventQueue("sn_hr_le.reminder",gr);
	
	}
	
}

 here is my updated script 

Ian Mildon
Tera Guru

While this example is not exactly what you are wanting it is a known working script example I have that is utilizing a schedule lookup. In this case it's a "round-robin" assignment rule that I created prior to the Advanced Work Assignment being created by SN.

 

(function executeRule(current, previous /*null when async*/ ) {

    //assign to the group check
    if (current.active == true && current.assignment_group == 'ca3475badb05b240ec5c3c00ad9619b2') { 
        var schedRec = new GlideRecord('cmn_schedule');
        schedRec.get('name', '8-5 weekdays excluding holidays'); //schedule name
		var sched = new GlideSchedule(schedRec.sys_id);
		
        if (sched.isInSchedule(new GlideDateTime()) && current.assigned_to == '') { //check schedule and assign
            var grMemRec = new GlideRecord('sys_user_grmember');
            grMemRec.addQuery('group', current.assignment_group);
            grMemRec.addQuery('user.u_primary_group', 'ca3475badb05b240ec5c3c00ad9619b2'); //only include if primary group is also ISD Service Desk
            grMemRec.addQuery('user.u_auto_assign', 'true'); //checkbox on profile
            grMemRec.orderBy('u_last_incident_assigned');
            grMemRec.query();
            if (grMemRec.next()) {
                current.assigned_to = grMemRec.user.getDisplayValue();
                grMemRec.u_last_incident_assigned = gs.nowDateTime();
                grMemRec.setWorkflow(false);
                grMemRec.update();
            }
        }
    }

})(current, previous);

You'll be most interested in the first part of the script, prior to the grmember lookup.