In notifications, message html 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 html by calling like $scriptname of that notification in servicenow
Assistant
To achieve the requirement where the notification date should be 10 days after the current date in ServiceNow, you'll need to create a script that calculates the date and then include this script in your email notification HTML. Here’s a step-by-step guide on how to do this:
User
Not working i think, when i click preview notification
Assistant
Create the Mail Script in ServiceNow
Steps to create a new Mail Script:
Navigate to System Notification > Email > Notification Email Scripts in ServiceNow.
Click on New to create a new email script.
Enter the following details:
Name: Something descriptive like Calculate 10 Day Later Date.
Script: Add the following script to calculate the date 10 days from now:
javascript
// Get the current GlideDateTime
var gdt = new GlideDateTime();
// Add 10 days to the current date
gdt.addDaysLocalTime(10);
// Format the date to the desired format (e.g., yyyy-MM-dd)
var formattedDate = gdt.getLocalDate().toString();
// Return the formatted date
answer = formattedDate;
This script sets the answer variable to the date 10 days from now in the format yyyy-MM-dd.
Save the script.
Include the Script in Your Notification Message HTML
Here’s how you can call the created script in your email notification:
Navigate to System Notification > Email > Notifications in ServiceNow.
Open the notification you want to modify.
In the What it will contain section, add or modify the Message HTML to include the script. You use the ${mail_script:your_script_name} syntax to call the script:
html
<html>
<body>
<p>Hello,</p>
<p>Your action is due on: ${mail_script:Calculate 10 Day Later Date}</p>
<p>Thank you!</p>
</body>
</html>
Using this approach, whenever the notification email is sent, the script will be executed, and the date 10 days from now will be dynamically inserted into the email message.
Note:
You can format the date differently based on your requirements. The getLocalDate() method in GlideDateTime returns a string in yyyy-MM-dd format; if you need a different format, you can use different methods available in the GlideDateTime class.
Ensure your script does not have any syntax errors and the correct name is being called in the email notification.
Let's debug and refine the process to make sure the notification works correctly when previewed.
Verify Mail Script:
Ensure that the mail script is returning the correct date. You can temporarily add some logging to help you debug. Update your mail script as follows:
javascript
// Get the current GlideDateTime
var gdt = new GlideDateTime();
// Log current date for debugging
gs.log("Current date: " + gdt.toString());
// Add 10 days to the current date
gdt.addDaysLocalTime(10);
// Format the date to the desired format (e.g., yyyy-MM-dd)
var formattedDate = gdt.getLocalDate().toString();
// Log the calculated date for debugging
gs.log("Date after 10 days: " + formattedDate);
// Return the formatted date
answer = formattedDate;
After adding the logging, preview the notification again and check the System Logs > All to see if the logs contain the expected output.
Verify Correct Usage in Notification:
Ensure the script name exactly matches what you have in the mail script. Here's a checklist:
Name exactly matches the mail_script reference.
There are no extra spaces around the script name.
Example:
html
<html>
<body>
<p>Hello,</p>
<p>Your action is due on: ${mail_script:Calculate 10 Day Later Date}</p>
<p>Thank you!</p>
</body>
</html>
Testing Mail Script Directly:
Another way to test the script is to run it directly in a background script to make sure it's producing the correct output. Navigate to System Definition > Scripts - Background and run the following:
javascript
// Get the current GlideDateTime
var gdt = new GlideDateTime();
// Log current date for debugging
gs.print("Current date: " + gdt.toString());
// Add 10 days to the current date
gdt.addDaysLocalTime(10);
// Format the date to the desired format (e.g., yyyy-MM-dd)
var formattedDate = gdt.getLocalDate().toString();
// Log the calculated date for debugging
gs.print("Date after 10 days: " + formattedDate);
// Return the formatted date
formattedDate;
This should print out the current date and the date 10 days from now.
Check Notification Conditions and Targets:
Ensure that the notification is set up with the correct conditions and targets. If the notification is not triggered under the conditions you're testing, it won't show the desired output.
Previewing Format:
Sometimes, the preview mode in ServiceNow might not fully execute the scripts. Make sure your notification is actually being sent out and check the email received.
Final Script:
Here's the refined mail script for final implementation:
javascript
// Get the current GlideDateTime object
var gdt = new GlideDateTime();
// Add 10 days to the current date/time
gdt.addDaysLocalTime(10);
// Format the date as needed
var formattedDate = gdt.getLocalDate().toString();
// Log the calculated date for debugging (optional)
gs.log("Calculated date 10 days from now: " + formattedDate);
// Set the script output
answer = formattedDate;
Once you ensure the script is working correctly (check logs and test outputs), continue to include this script in your notification message.
If the above checks still do not work, try hosting the script content in a simpler HTML notification content and test the notifications under realistic scenarios (e.g., send it to a test email).