Send Notification 5 Business Days before Due Date

Lavanya Nagendr
Giga Guru

Hi All,

 

I have a requirement where we need a schedule to run daily, which finds records that will be due in next 5 business days and sends out notification.

Note: The due date can also be in weekends.

 

Schedule Script:( For this script the notification is triggered for all records in the Problem task table)

var days = 5;
var startDate = new GlideDateTime();
var days1 = parseInt(days); //assuming there are 8 business hours
var dur = new GlideDuration(60 * 60 * 1000 * 24 * days1); 
var schedule = new GlideSchedule('sys_id');  //put your schedule sys_id here
var end = schedule.add(startDate, dur);
gs.log('end' + end.getDate());
var notif = new GlideRecord('problem_task');
notif.addQuery('active' , 'true');
//notif.addEncodedQuery('due_dateON'+end.getDate());
notif.query();
while(notif.next()){
if(notif.due_date.getDayOfWeek() < 6){
gs.log('triggered');   
gs.eventQueue('problem_task.duedate.notify',notif);
}
}

 

 Any help is appreciated!

 

Thanks!

Lavanya

1 REPLY 1

Sid_Takali
Kilo Patron
Kilo Patron

Hi @Lavanya Nagendr Try below code

var days = 5;
var startDate = new GlideDateTime(); 
var endDate = new GlideDateTime(startDate); 
var businessDaysToAdd = days;
var businessDaysAdded = 0;

while (businessDaysAdded < businessDaysToAdd) {
    endDate.addDays(1); 
    if (endDate.getDayOfWeek() != 1 && endDate.getDayOfWeek() != 7) { 
        businessDaysAdded++;
    }
}
var notif = new GlideRecord('problem_task');
notif.addQuery('active', true);
notif.addQuery('due_date', '>=', startDate);
notif.addQuery('due_date', '<=', endDate);
notif.query();

while (notif.next()) {
    if (notif.due_date.getDayOfWeek() != 1 && notif.due_date.getDayOfWeek() != 7) { 
        gs.eventQueue('problem_task.duedate.notify', notif); 
    }
}