Email script throughing an unexpected error that it's giving same day for all records

1_DipikaD
Kilo Sage

Hi All,

I have a notification which is triggered by a event and that event is triggered by a scheduled job. And also have email script in that notification. If you see the below email script :- Suppose  10 records falls under this  notification , now same days are coming for all the records but ideally it should come days remaining for all the records individually.

 
Thank you
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@1_DipikaD 

try this -> moved the calculation inside the loop

(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 reviewdateValue = reviewdate.getNumericValue();

        var gdt = new GlideDateTime();
        var gdt1 = gdt.getNumericValue();

        var dateDiff = reviewdateValue - gdt1; // Still in milliseconds here
        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);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@1_DipikaD 

try this -> moved the calculation inside the loop

(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 reviewdateValue = reviewdate.getNumericValue();

        var gdt = new GlideDateTime();
        var gdt1 = gdt.getNumericValue();

        var dateDiff = reviewdateValue - gdt1; // Still in milliseconds here
        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);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

 

I am attaching a picture of preview email and the schedule job script for better understanding. If you see the days untill breach is same for all the records i.e 13 but it should be remaining days  And also i want to trigger simillar notification before 7 days of due date .

 

 

@1_DipikaD 

you are calculating the date in email script.

I shared the updated email script above

Did you try that?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader