Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Issue with my script to anticipate Monday closure

Axel5
Kilo Guru

Hello,

 

I'm trying to solved the issue with my scheduled script, I need to anticipate the Friday any RITM with due date expected for the next Monday and trigger the event to send a notification. Here is the code :

 

(function() {
    
    var tomorrow = new GlideDateTime();
    tomorrow.addDaysUTC(1);
    
    var dayOfWeek = tomorrow.getDayOfWeekUTC();
    
    // First query: Always check for ritm due tomorrow
    var gr = new GlideRecord('sc_req_item');
    gr.addQuery('active', true);
    gr.addQuery('cat_item.name', 'Environment_Closure');
    gr.addQuery('due_date', 'ON', tomorrow);
    gr.query();
    
    while (gr.next()) {
        gs.eventQueue('event_AppSup_task_closure_reminder', gr, gs.getUserName(), gs.getUserID());
    }
    
    // Second query: If tomorrow is Saturday or Sunday, also check for ritm due next Monday
    if (dayOfWeek == 6 || dayOfWeek == 0) { // Saturday or Sunday
        
        var nextMonday = new GlideDateTime(tomorrow);
        
        if (dayOfWeek == 6) {
            // If tomorrow is Saturday, next Monday is 2 days later
            nextMonday.addDaysUTC(2);
        } else if (dayOfWeek == 0) {
            // If tomorrow is Sunday, next Monday is 1 day later
            nextMonday.addDaysUTC(1);
        }
        
        // Query for records with due date on next Monday
        var grMonday = new GlideRecord('sc_req_item');
        grMonday.addQuery('active', true);
        grMonday.addQuery('cat_item.name', 'Environment_Closure');
        grMonday.addQuery('due_date', 'ON', nextMonday);
        grMonday.query();
        
        while (grMonday.next()) {
            gs.eventQueue('event_AppSup_task_closure_reminder', grMonday, gs.getUserName(), gs.getUserID());
        }
    }
    
})();

 

I did some test but no results.. what do you think about that ?

 

Thanks for help.

 

Regards, 

1 ACCEPTED SOLUTION

pr8172510
Giga Guru

Hi @Axel5,

Your approach is close, but there are a couple of common pitfalls with GlideDateTime and the ON operator that are likely causing no results.

(function() {

var today = new GlideDateTime();
var todayDay = today.getDayOfWeekUTC(); // 1=Mon ... 5=Fri ... 7=Sun

// ---------- TOMORROW ----------
var tomorrowStart = new GlideDateTime();
tomorrowStart.addDaysUTC(1);
tomorrowStart.setHourUTC(0);
tomorrowStart.setMinuteUTC(0);
tomorrowStart.setSecondUTC(0);

var tomorrowEnd = new GlideDateTime(tomorrowStart);
tomorrowEnd.addDaysUTC(1);

var gr = new GlideRecord('sc_req_item');
gr.addQuery('active', true);
gr.addQuery('cat_item.name', 'Environment_Closure');
gr.addQuery('due_date', '>=', tomorrowStart);
gr.addQuery('due_date', '<', tomorrowEnd);
gr.query();

while (gr.next()) {
gs.eventQueue('event_AppSup_task_closure_reminder', gr, '', '');
}

// ---------- MONDAY (IF FRI/SAT/SUN) ----------
if (todayDay == 5 || todayDay == 6 || todayDay == 7) {

var mondayStart = new GlideDateTime();

if (todayDay == 5) { // Friday
mondayStart.addDaysUTC(3);
} else if (todayDay == 6) { // Saturday
mondayStart.addDaysUTC(2);
} else { // Sunday
mondayStart.addDaysUTC(1);
}

mondayStart.setHourUTC(0);
mondayStart.setMinuteUTC(0);
mondayStart.setSecondUTC(0);

var mondayEnd = new GlideDateTime(mondayStart);
mondayEnd.addDaysUTC(1);

var grMonday = new GlideRecord('sc_req_item');
grMonday.addQuery('active', true);
grMonday.addQuery('cat_item.name', 'Environment_Closure');
grMonday.addQuery('due_date', '>=', mondayStart);
grMonday.addQuery('due_date', '<', mondayEnd);
grMonday.query();

while (grMonday.next()) {
gs.eventQueue('event_AppSup_task_closure_reminder', grMonday, '', '');
}
}

})();



View solution in original post

3 REPLIES 3

Tanushree Maiti
Kilo Patron

Hi @Axel5 ,

 

At our end, we are currently using the following snippet:

 

var dayOfWeek = new GlideDateTime().getDate().getDayOfWeek();    


This appears to be working fine for us.

 

You may also want to consider adding additional logging (gs.log) to help identify exactly where the issue might be occurring.

 

 

Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin:

pr8172510
Giga Guru

Hi @Axel5,

Your approach is close, but there are a couple of common pitfalls with GlideDateTime and the ON operator that are likely causing no results.

(function() {

var today = new GlideDateTime();
var todayDay = today.getDayOfWeekUTC(); // 1=Mon ... 5=Fri ... 7=Sun

// ---------- TOMORROW ----------
var tomorrowStart = new GlideDateTime();
tomorrowStart.addDaysUTC(1);
tomorrowStart.setHourUTC(0);
tomorrowStart.setMinuteUTC(0);
tomorrowStart.setSecondUTC(0);

var tomorrowEnd = new GlideDateTime(tomorrowStart);
tomorrowEnd.addDaysUTC(1);

var gr = new GlideRecord('sc_req_item');
gr.addQuery('active', true);
gr.addQuery('cat_item.name', 'Environment_Closure');
gr.addQuery('due_date', '>=', tomorrowStart);
gr.addQuery('due_date', '<', tomorrowEnd);
gr.query();

while (gr.next()) {
gs.eventQueue('event_AppSup_task_closure_reminder', gr, '', '');
}

// ---------- MONDAY (IF FRI/SAT/SUN) ----------
if (todayDay == 5 || todayDay == 6 || todayDay == 7) {

var mondayStart = new GlideDateTime();

if (todayDay == 5) { // Friday
mondayStart.addDaysUTC(3);
} else if (todayDay == 6) { // Saturday
mondayStart.addDaysUTC(2);
} else { // Sunday
mondayStart.addDaysUTC(1);
}

mondayStart.setHourUTC(0);
mondayStart.setMinuteUTC(0);
mondayStart.setSecondUTC(0);

var mondayEnd = new GlideDateTime(mondayStart);
mondayEnd.addDaysUTC(1);

var grMonday = new GlideRecord('sc_req_item');
grMonday.addQuery('active', true);
grMonday.addQuery('cat_item.name', 'Environment_Closure');
grMonday.addQuery('due_date', '>=', mondayStart);
grMonday.addQuery('due_date', '<', mondayEnd);
grMonday.query();

while (grMonday.next()) {
gs.eventQueue('event_AppSup_task_closure_reminder', grMonday, '', '');
}
}

})();



Axel5
Kilo Guru

Thanks a lot for your both feedback, now it is working smoothly.

 

You rock !