Email Script (To print dynamic days through Email notification)

Naman Jain2412
Tera Expert

Hi, 
I have created a below script however when I am triggering notification it is showing days like that in format 14141415. Could you please let me know what is the specific issue because of which it is showing like this. 

Name of the email script :- sn_dynamic_values_renewal_date

Below is the attached code for reference: -

 

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
        /* Optional EmailOutbound */
        email, /* Optional GlideRecord */ email_action,
        /* Optional GlideRecord */
        event) {

        var user;
        var gr = new GlideRecord('sn_compliance_policy');
        gr.addQuery("state", "draft");
        gr.query();
        while (gr.next()) {


            var u = new GlideDateTime(gr.u_next_renewal_date);
            var c = new GlideDateTime().getDate();
            var d = gs.dateDiff(c, u, true);
            var days = d / (60 * 60 * 24);
            // var days = parseInt(d,10)/(60*60*24);

         

                if (days == 15 || days == 14 ||days == 12 ||days == 10 ||days == 8 || days == 6 ||days == 4 ||days == 2||days == 0) {
                    // template.print("u_next_renewal_date" + days + " days<br/>");
                        template.print(days);

                }

            // Add your code here

        }
   
})(current, template, email, email_action, event);
 
In Notification 
This is to inform you that the ${name} is scheduled for renewal on ${u_next_renewal_date}, which is ${mail_script:sn_dynamic_values_renewal_date} days from today.

Eg :- Renewal date is scheduled on 09/07/2026

and the TEST Record is created on 09/07/2025 14:03:00

Based on the given script it is showing result as 0206101012 days as 

 

 

This is to inform you that the TEST is scheduled for renewal on 09/07/2026, which is 0206101012 days from today.

 

 

10 REPLIES 10

@Naman Jain2412 

We are not sure how you are triggering the email.

I believe you are using some scheduled job, if that's the case then you should get details only for that record and print the details.

Please share some more details about your requirement, screenshots of what you configured etc

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

Yes, I am triggering through the scheduled job but multiple records are meeting the criteria and in the notification, it is showing as 10101012 something like that. I would like to show specific days for a particular day. Like I have created Test which is 45 days renewal date based on current date

And Another record Test1 which is 15 days renewal date based on current date then when I will execute schedule job then it show 45 days for Test and 15 days Test1 due for renewal in notification. I hope you are able to understand otherwise I will elaborate more on this. pls let me know.

 

I have added the Scheduled Job for your reference

var user;
var gr = new GlideRecord('sn_compliance_policy');
gr.addQuery("state", "draft");
gr.query();
while (gr.next()) {


    var u = new GlideDateTime(gr.u_next_renewal_date);
    var c = new GlideDateTime().getDate();
    var d = gs.dateDiff(c, u, true);
    var days = d / (60 * 60 * 24);



    if (days == 90) {
        gs.eventQueue('sn_renewal.event.policy', gr, gr.getDisplayValue('u_custodian_name'));
        // gs.log("900 completed");


    } else if (days == 45) {
        gs.eventQueue('sn_renewal.event.policy', gr, gr.getDisplayValue('u_custodian_name'));

    } else if (days == 15) {
        gs.eventQueue('sn_renewal.event.policy', gr, gr.getDisplayValue('u_custodian_name'));

    } else if (days == 14 || days == 12 || days == 10 || days == 8 || days == 6 || days == 4 || days == 2 || days == 0) {

        gs.eventQueue('sn_event_renewal_alternate_policy', gr, gr.getDisplayValue('u_custodian_l4_name'), gr.getDisplayValue('u_custodian_name'));

    } else {
        gs.log("hello end");


    }


}
 
Email Script 

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,

        /* Optional EmailOutbound */

        email, /* Optional GlideRecord */ email_action,

        /* Optional GlideRecord */

        event) {

        var user;

        var gr = new GlideRecord('sn_compliance_policy');

        gr.addQuery("state", "draft");

        gr.query();

        while (gr.next()) {

 

            var u = new GlideDateTime(gr.u_next_renewal_date);

            var c = new GlideDateTime().getDate();

            var d = gs.dateDiff(c, u, true);

            var days = d / (60 * 60 * 24);

            // var days = parseInt(d,10)/(60*60*24);

         

                if (days == 15 || days == 14 ||days == 12 ||days == 10 ||days == 8 || days == 6 ||days == 4 ||days == 2||days == 0) {

                    // template.print("u_next_renewal_date" + days + " days<br/>");

                   

                        template.print(days);

                }

            // Add your code here

        }

   

})(current, template, email, email_action, event);

 



@Naman Jain2412 

then use current object in the email script I shared above.

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

could you pls let me know where to add current and the code shared by you will trigger if days matches with 15 , 14 , 13 , 12 , 11. so on or it will trigger only specific record.

 

@Naman Jain2412 

Did my reply answer your question?

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