Notification getting triggered for unexpected records and script throughs wrong calculation

1_DipikaD
Kilo Sage

Hi All,

I have schedule job which triggers a event and that event triggers a notification. The contains a email script.

Now I want send notification to the users when review date of the records remains 7days or 14days away from notification triggered date (only for 7days and 14 days nothing else). If you see the pictures it's fetching 5th may records and 12th may records . I mean the records which have due date on 5th and 12th but ideally it should fetch 6th may and 13th may records if you calculate from today(29th April). And Second thing the days until breach column should calculate only 7days and 14 days not 3/8/18/6/13 etc. And the records in the email should show according to those days. Recently, I noticed that the email triggered date is not taking today's date. If you the email picture attached below email created date is today(29.04.2025) but while I preview email it's showing 28.04.2025 . I am attaching the schedule job and Email script for your reference. Please help me out on this.

 

Thank you 

 

Schedule Job :- Runs daily

 

var gr_user = new GlideRecord('u_capacity_reviews');
gr_user.addEncodedQuery('u_status=pending^active=true');
gr_user.query();
while (gr_user.next()) {
    var reviewdate = new GlideDateTime(gr_user.u_capacity_review_due_date.toString());
    reviewdate.addDaysLocalTime(parseInt(-13));
 
    var duedate = reviewdate.getDisplayValue().split(' ')[0];
 
    var gr_date = new GlideDate();
 
    if (gr_date.toString() == duedate.toString()) {
 
        gs.eventQueue('notify.capacity.reviewers', gr_user, gr_user.u_application_instance.u_support_owner, gr_user.u_application_instance.owned_by);
   
    }
   var reminder = new GlideDateTime(gr_user.u_capacity_review_due_date.toString());
    reminder.addDaysLocalTime(-6);
    var duedate2 = reminder.getDisplayValue().split(' ')[0];
if (gr_date.toString() == duedate2.toString()) {
        gs.eventQueue('notify.capacity.reviewers', gr_user, gr_user.u_application_instance.u_support_owner, gr_user.u_application_instance.owned_by);
       
   }
 
}

 

 

Email Script :-

 

(function runMailScript(current, template, email, email_action, event) {

    var gr = new GlideRecord('u_capacity_reviews');
    gr.addQuery('u_status', 'pending');
    gr.addQuery('u_application_instance.u_support_owner', current.u_application_instance.u_support_owner);
    gr.query();

    if (gr.hasNext()) {
        template.print('<table class ="grplist"><tr style="font-size: 9pt; background: rgb(193, 193, 193); border:1pt solid black; text-align: center;"><td style="font-size: 8.5pt; width: 10%;"><strong>Division</strong></td><td style="font-size: 8.5pt; width: 10%;"><strong>Subdivision</strong></td><td style="font-size: 8.5pt; width: 10%;"><strong>Number</strong></td><td style="font-size: 8.5pt; width: 10%;"><strong>Application Instance</strong></td><td style="font-size: 8.5pt; width: 10%;"><strong>Due Date</strong></td><td style="font-size: 8.5pt; width: 10%;"><strong>Minimum Review Cycle</strong></td><td style="font-size: 8.5pt; width: 10%;"><strong>Support Group</strong></td><td style="font-size: 8.5pt; width: 10%;"><strong>Days until Breach</strong></td></tr>');
    }

    while (gr.next()) {
    var reviewDate = new GlideDateTime(gr.getValue('u_capacity_review_due_date'));
var today = new GlideDateTime();

reviewDate.setDisplayValue(reviewDate.getDate().getDisplayValue());
today.setDisplayValue(today.getDate().getDisplayValue());

var reviewdateValue = reviewDate.getNumericValue();
var todayValue = today.getNumericValue();

var dateDiff = reviewdateValue - todayValue;
var daysDiff = dateDiff / (24 * 60 * 60 * 1000);
var daysDiff1 = Math.floor(daysDiff);

        var capacity_number = '<a href="http://' + gs.getProperty("instance_name") + '.service-now.com/u_capacity_reviews.do?sys_id=' + gr.getValue('sys_id') + '">' + gr.u_number + '</a>';
        var appinstance = '<a href="http://' + gs.getProperty("instance_name") + '.service-now.com/u_cmdb_ci_app_instance.do?sys_id=' + current.u_application_instance.sys_id + '">' + gr.u_application_instance.name + '</a>';

        template.print('<tr><td style="font-size: 8.5pt; width: 10%;">' + gr.u_application_instance.u_division.name + '</td><td style="font-size: 8.5pt; width: 10%; " >' + gr.u_application_instance.u_subdivision.name + '</td><td style="font-size: 8.5pt; width: 10%; " >' + capacity_number + '</td><td style="font-size: 8.5pt; width: 10%;">' + appinstance + '</td><td style="font-size: 8.5pt; width: 10%;">' + gr.u_capacity_review_due_date + '</td><td style="font-size: 8.5pt; width: 10%;">' + gr.u_review_cycle + '</td><td style="font-size: 8.5pt; width: 10%;">' + gr.u_application_instance.u_support_group.name + '</td><td style="font-size: 8.5pt; width: 10%;">' + daysDiff1 + '</td></tr>');
    }

    template.print('</table>');
    template.print('<style>.grplist td{border-spacing: 0pt; padding: 0mm 0mm 0mm .5mm; border:1pt solid black;}</style>');
    template.print('<style>.grplist{border-spacing: 0px; border-collapse: collapse; border:1pt solid black;}</style>');

})(current, template, email, email_action, event);
 
 
 
1_DipikaD_2-1745878575939.png

 

 

1_DipikaD_0-1745878262899.png

1_DipikaD_1-1745878471907.png

 

7 REPLIES 7

Yep we shouldn't change that. I just wanted to know your system timezome. So it's Europe/London.

May I know at what time your schedule job runs?

both

 

1_DipikaD
Kilo Sage

Also I saw while email is getting triggered now , it's taking the triggered date correct as it was coming incorrect earlier. But still it's taking incorrect records and calculating the days wrong .

1_DipikaD_0-1745910803885.png