In notification, message we want to populate the date after 10 days of notification send. If notification is sent today then the date in notification message should be 10 days after the today date. I need to write the email script for this and use it in message of that notification in servicenow. I want to use this date shown between the text message
Assistant
Sure, you can accomplish this in ServiceNow by using a scripted email notification. You'll need to use the GlideDateTime class to calculate and format the date that is 10 days in the future. Below is an example of how you could script this:
// Define the GlideDateTime object for the current date
var currentDate = new GlideDateTime();
// Add 10 days to the current date
currentDate.addDays(10);
// Format the date to a readable format, e.g., "dd-MMM-yyyy"
var futureDate = currentDate.getDate().getByFormat('dd-MMM-yyyy');
// Create your message, embedding the future date within the text
var message = "This notification is sent today. Please take action by " + futureDate + ".";
// Use the message in the email notification
email.body = message;
You can insert the above script into the "Advanced" section of your email notification in ServiceNow. Here's how to do it step-by-step:
User
I have my message in message html . I call it in message html
Assistant
Got it! If you're working within the HTML message body of an email notification in ServiceNow, you can include the script directly within the notification's script field and then reference the variable in your HTML. Here's how to do it:
Navigate to System Notification > Email > Notifications.
Create a new notification or select an existing one.
Under the "What it will contain" tab, ensure "Advanced" checkbox is checked.
Scroll down to the "Script" field.
Copy the above script and paste it into the Script field.
Ensure your message includes the dynamic date in the context of your full notification message. For example:
email.body = "Hello User, \n\nThis is a reminder that your action is required by " + futureDate + ". \n\nThank you!";
This allows you to dynamically insert the calculated date into your email content. The email system will then handle sending this message with the correct calculated date.
Script:
Let's fill the email.body with HTML content, embedding the calculated future date.
Advanced Notification Script:
Ensure the "Advanced" checkbox is checked in your email notification.
Use the script below to calculate the future date and store it in a variable:
// Define the GlideDateTime object for the current date
var currentDate = new GlideDateTime();
// Add 10 days to the current date
currentDate.addDays(10);
// Format the date to a readable format, e.g., "dd-MMM-yyyy"
var futureDate = currentDate.getDate().getByFormat('dd-MMM-yyyy');
// Store the date in a scratchpad variable for use in the HTML template
template.print('<g:evaluate var="futureDate">');
template.print(futureDate);
template.print('</g:evaluate>');
HTML Template:
Now, you need to include the futureDate variable in your message HTML.
Message HTML:
You can reference the futureDate variable directly within your HTML content.
<html>
<body>
<p>Hello User,</p>
<p>This is a reminder that your action is required by <strong>${futureDate}</strong>.</p>
<p>Thank you!</p>
</body>
</html>
Bringing It All Together:
So, the complete notification setup is:
Notification Script:
In the Script field of the email notification:
var currentDate = new GlideDateTime();
currentDate.addDays(10);
var futureDate = currentDate.getDate().getByFormat('dd-MMM-yyyy');
template.print('<g:evaluate var="futureDate">');
template.print(futureDate);
template.print('</g:evaluate>');
Message HTML:
In the Message HTML field of the email notification:
<html>
<body>
<p>Hello User,</p>
<p>This is a reminder that your action is required by <strong>${futureDate}</strong>.</p>
<p>Thank you!</p>
</body>
</html>
By following these steps, you should be able to dynamically calculate a date 10 days in the future and include it within the HTML content of your ServiceNow email notification.