Email Script (To print dynamic days through Email notification)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-13-2025 10:16 PM
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: -
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-13-2025 10:33 PM
Hi @Naman Jain2412
Try the below script and see how it goes..
(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 endDate = new GlideDateTime(gr.u_next_renewal_date);
var startDate = new GlideDateTime();
// Get the difference in milliseconds
var diffMillis = endDate.getNumericValue() - startDate.getNumericValue();
// Convert milliseconds to days
var diffDays = diffMillis / (1000 * 60 * 60 * 24);
var days = Math.floor(diffDays);
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);
}
}
})(current, template, email, email_action, event);
Regards,
Siva
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-13-2025 10:55 PM
I just applied the same code however now it is showing the same for every record
This is to inform you that the COLOR CODE TEST is scheduled for renewal on 28/08/2025, which is 10101010 days from today.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-13-2025 11:09 PM - edited 07-13-2025 11:12 PM
@Naman Jain2412 I echo @Mark Manders .. Try the below script and see..
If you're trying to trigger an email for each policy individually, you need to use the current object.
(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */
email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */
event) {
if (current.state == 'draft') {
var endDate = new GlideDateTime(current.u_next_renewal_date);
var startDate = new GlideDateTime();
// Get the difference in milliseconds
var diffMillis = endDate.getNumericValue() - startDate.getNumericValue();
// Convert milliseconds to days
var diffDays = diffMillis / (1000 * 60 * 60 * 24);
var days = Math.floor(diffDays);
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);
}
}
})(current, template, email, email_action, event);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-13-2025 10:58 PM
notification is on which table? how are you triggering it?
You are iterate all the records in that email script.
Do you require all or some records?
Use this -> Recommended if you expect only one relevant policy per notification
(function runMailScript(current, template, email, email_action, event) {
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();
var duration = GlideDateTime.subtract(c, u);
var days = parseInt(duration.getNumericValue() / 86400000);
// If days matches one of your values, print and break
if ([15, 14, 12, 10, 8, 6, 4, 2, 0].indexOf(days) !== -1) {
template.print(days);
break; // Stop after first match
}
}
})(current, template, email, email_action, event);
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader