in servicenow, i need to write email script to show next 2 days from current date
- User
in servicenow, i need to write email script to show next 2 days from current date
- Assistant
In ServiceNow, you can create email notifications and scripts using JavaScript. If you want to generate the dates for the next two days from the current date, you can achieve that with a simple script. Here's an example script that you can use in an email notification:
// Get the current date var currentDate = new GlideDateTime(); // Current date and time var nextDay1 = new GlideDateTime(currentDate); var nextDay2 = new GlideDateTime(currentDate); // Add 1 day for the next day nextDay1.addDays(1); // Add 2 days for the day after next nextDay2.addDays(2); // Format the dates (you can customize the format) var nextDay1Formatted = nextDay1.getDisplayValue(); var nextDay2Formatted = nextDay2.getDisplayValue(); // Create the email message var emailBody = "The next two days are:\n"; emailBody += "1. " + nextDay1Formatted + "\n"; emailBody += "2. " + nextDay2Formatted; // You can send this email body or use it in your notification email.body += emailBody; - User
if today date is 26 then i need to show 28 with 2 days add in notification
- Assistant
If you specifically want to display the date two days from today (consistently adding two days regardless of the current date), the script needs to ensure that it adds two days correctly and formats the output accordingly. Below is the modified script to achieve this:
// Get the current date var currentDate = new GlideDateTime(); // Current date and time var twoDaysLater = new GlideDateTime(currentDate); // Add 2 days twoDaysLater.addDays(2); // Format the date (you can customize the format) var twoDaysLaterFormatted = twoDaysLater.getDisplayValue(); // Create the email message var emailBody = "The date two days from today is:\n"; emailBody += twoDaysLaterFormatted; // You can send this email body or use it in your notification email.body += emailBody;Explanation:
- GlideDateTime(): Initializes the current date and time.
- : Adds two days to the current date.
- User
not working
- Assistant
I apologize for the inconvenience. Let’s ensure that we capture the requirement correctly and provide you with a clear, functioning script that will work within the context of ServiceNow email notifications.
Full Email Script Example
Here's a modified version of the script that should correctly add 2 days to the current date and format it in the email notification:
- User
we use this like ${email_script:datepopulate} in the body, but showing same nam
- Assistant
- User
(function runMailScript( /* GlideRecord / current, / TemplatePrinter / template, / Optional EmailOutbound / email, / Optional GlideRecord / email_action, / Optional GlideRecord */ event) { // Get the current date // Get the current date // Create a new GlideDateTime object for the current date and time var currentDate = new GlideDateTime();
// Create a new GlideDateTime object for the two days later date var twoDaysLater = new GlideDateTime(currentDate); // Add 2 days to the current date twoDaysLater.addDays(2); // Get the date in a specified format (e.g., 'YYYY-MM-DD') var twoDaysLaterFormatted = twoDaysLater.getDate(); // Retrieves just the date part in 'yyyy-MM-dd' var twoDaysLaterHumanReadable = twoDaysLater.getDisplayValue(); // For a human-friendly format // Return the human-readable date return "The date two days from today is: " + twoDaysLaterHumanReadable;})(current, template, email, email_action, event);
is this correct?
- Assistant
- User
it is showing blank
- Assistant