Creating an email notification in ServiceNow that prints a date 10 days from the current date formatted as "27-May-2024" involves using the scripting capabilities within the ServiceNow platform. You can achieve this by using the GlideDateTime API and JavaScript within the email template or notification script.
Here's a step-by-step guide to creating this email script in ServiceNow:
Step 1: Define the Notification
- Navigate to: System Notification > Email > Notifications.
- Click on "New" to create a new notification.
- Fill in the required fields for your notification, such as Name, Table, When to send, etc.
Step 2: Add the Script to the Email Template
In the email body, you can use the following script to calculate the date 10 days from today and format it as "27-May-2024".
Use ${} syntax to embed server-side scripts in your email template.
<html>
<body>
<p>The date 10 days from today is: ${calculateFutureDate(10)}</p>
</body>
</html>
Step 3: Define the calculateFutureDate Function
You need to define this function in a way that it can be called from the email notification. This is done by creating a mail script that the notification can use.
- Navigate to: System Notification > Email > Mail Scripts.
- Click on "New" to create a new mail script.
- Name the script appropriately, for example, "Calculate Future Date".
- Add the following script to calculate the future date and format it.
(function(daysToAdd) {
var gdt = new GlideDateTime();
gdt.addDaysUTC(daysToAdd); // Add the specified number of days
var gd = gdt.getLocalDate(); // Get the date part
var formattedDate = gd.getByFormat('dd-MMM-yyyy'); // Format the date
return formattedDate;
})(daysToAdd);
Step 4: Reference the Script in the Email Template
Now that you have your mail script ready, you can reference it in the email template:
- Edit the email notification you created.
- In the "What it will contain" section, add the following:
<html>
<body>
<p>The date 10 days from today is: ${MailScriptNameNameHere(10)}</p>
</body>
</html>
Replace MailScriptNameHere with the actual name of your mail script.
Step 5: Set Up Email Notification Trigger
Ensure your email notification is set to trigger based on the desired conditions.
Example Email Body
Here’s how the email might look when it sends:
<html>
<body>
<p>The date 10 days from today is: 27-May-2024</p>
</body>
</html>
By following these steps, ServiceNow will generate the date 10 days in the future, format it according to your specifications, and include it in your email notifications. Make sure to test the notification to ensure it's working as expected.