In email script i want to show the date 10 days more from todays date. If the date is 1 today then i need to show 11 .. servicenow
Assistant
In ServiceNow, if you want to generate an email script that includes a date 10 days from today's date, you'll need to use GlideDateTime to manipulate date values. Here is an example of how you might do this:
Create an Email Notification: First, ensure that you have created an email notification that you want to script.
User
I need to show the this date in notification message only. If the notification is sent on 2nd july then in message it will show= please approve this before //+10 days date via ${mail_script}
Assistant
To include the calculated date directly in the notification message using a ${mail_script}, you can create an email script in ServiceNow and then reference it in your notification. Here’s how you can achieve this:
Create a Mail Script:
Navigate to in ServiceNow.
Add Email Script: Add an email script to your notification.
Script Example: Use the following script in your email notification to calculate and display the date 10 days from today.
javascript
// Create a GlideDateTime object for the current date
var gdt = new GlideDateTime();
// Add 10 days to the current date
gdt.addDays(10);
// Format the date as needed, here we're using the default display value
var futureDate = gdt.getLocalDate().toString();
email.addAddress("to", "example@example.com"); // Add recipient's email address
email.setSubject("Future Date Notification"); // Set email subject
email.setBody("The date 10 days from today is: " + futureDate); // Set email body
Steps Breakdown:
Creating GlideDateTime instance: var gdt = new GlideDateTime(); This initializes a GlideDateTime object with the current date and time.
Adding 10 days: gdt.addDays(10); The addDays method adjusts the date by the specified number of days.
Formatting the date: var futureDate = gdt.getLocalDate().toString(); This converts the date to a string in the local timezone format.
Email attributes: Use email.addAddress, email.setSubject, and email.setBody to set the recipient, subject, and body of the email respectively.
Note
Ensure you replace "example@example.com" with the actual recipient email address.
Customize the subject and the body of the email as needed.
If you are using this in a different context like a business rule, scheduled job or another script, adjust the email sending part to fit that specific context.
By using this script, every time the email notification is triggered, it will dynamically calculate and display the date 10 days from the current date.
System Notification > Email > Administration > Email Scripts
Click on New to create a new email script.
Define the Mail Script:
Provide a Name (e.g., FutureDateScript).
Write your script to calculate the date 10 days from today. Here’s an example of what the script might look like:
javascript
// Create a GlideDateTime object for the current date
var gdt = new GlideDateTime();
// Add 10 days to the current date
gdt.addDays(10);
// Format the date as needed, here we're using the default display value
var futureDate = gdt.getLocalDate().toString();
// Return the calculated future date
template.print(futureDate);
Save the Email Script:
Save the script after providing necessary details and ensuring the script is correct.
Use Email Script in a Notification Message:
Navigate to System Notification > Email > Notifications.
Open the notification where you want to include the future date.
In the Message field of the notification, reference your email script with ${mail_script:<Your_Script_Name>}.
For example:
plaintext
Please approve this before ${mail_script:FutureDateScript}
Complete Example for the Notification Message:
If today is July 2nd and you want to send a notification that includes July 12th, your notification message will appear like this:
plaintext
Please approve this before ${mail_script:FutureDateScript}
When the notification is sent, the ${mail_script:FutureDateScript} will be replaced by the date calculated in the script, 10 days from the current date.
Summary
By using a mail script, you can dynamically calculate the date and embed it into your notification messages. This method allows you to maintain clean and easily configurable notifications without hardcoding dates.