- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2024 08:38 AM - edited 11-07-2024 10:08 AM
Hello ServiceNow Community!
I have an email notification that inform users when their software license is soon to expire in < 30 days. The email body says "Records show your software license is due to expire on MM-DD-YYYY"
How do I code my mail script for this text to dynamically change based on the current date in relation to the expiration date?
For instance:
- If the expiration date is 12-01-2024 and today's date is 11-01-2024, the text should say "Records show your software license is due to expire on 12-01-2024."
- If the expiration date is 12-01-2024 and today's date is 12-22-2024, the text should say "Records show your software license has expired on 12-01-2024."
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-08-2024 10:30 AM - edited 11-08-2024 01:50 PM
I was able to accomplish this with a different code structure, in case anyone else was wondering.
// Get the expiration date and convert it to days to compare it to today's date to determine what text to send
var expirationDate = new GlideDateTime(current.expiration_date);
var gdt = new GlideDateTime();
var todayDate = gdt.getLocalDate();
var dateDifferenceInMs = expirationDate.getNumericValue() - GlideDateTime(todayDate).getNumericValue();
var dateDifferenceInDays = Math.floor(dateDifferenceInMs / (1000 * 60 * 60 * 24));
var daysUntilExpired = dateDifferenceInDays.toString().split('.');
// Get the expiration date and convert it to MM-DD-YYYY
var expireDate = expiration_date.getDate();
var gd = new GlideDate();
gd.setValue(expireDate.toString());
// Change the text based on the calculation above
var message;
if (daysUntilExpired >= 0) {
message= "Records show your software license is due to expire on " + expireDate.getByFormat("MM-dd-yyy") + ".";
} else {
message= "Records show your software license has expired on " + expireDate.getByFormat("MM-dd-yyy") + ".";
}
template.print(message);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2024 09:58 AM - edited 11-07-2024 09:58 AM
@neil_b You can probably try this below code to achieve same :
// Retrieve the expiration date from the license record
var expirationDate = new GlideDateTime(current.field_expiration_date); // Ensure 'field_expiration_date' is the correct
var today = new GlideDateTime();
var daysToExpiration = GlideDateTime.subtract(expirationDate, today).getDayPart();
// Format the expiration date as MM-DD-YYYY
var expirationDateFormatted = expirationDate.getByFormat("MM-dd-yyyy");
// Determine the message text
var message;
if (daysToExpiration >= 0) {
message = "Records show your software license is due to expire on " + expirationDateFormatted + ".";
} else {
message = "Records show your software license has expired on " + expirationDateFormatted + ".";
}
// Output the message to display in email
template.print(message);
Hope this will help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2024 10:07 AM
Thank you so much @Abhay Kumar1! I will try this and report back soon!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2024 01:52 PM - edited 11-07-2024 02:00 PM
@Abhay Kumar1 I think this line of code is not working properly. I tried to troubleshoot and do gs.info and this variable isn't returning any data.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-08-2024 10:30 AM - edited 11-08-2024 01:50 PM
I was able to accomplish this with a different code structure, in case anyone else was wondering.
// Get the expiration date and convert it to days to compare it to today's date to determine what text to send
var expirationDate = new GlideDateTime(current.expiration_date);
var gdt = new GlideDateTime();
var todayDate = gdt.getLocalDate();
var dateDifferenceInMs = expirationDate.getNumericValue() - GlideDateTime(todayDate).getNumericValue();
var dateDifferenceInDays = Math.floor(dateDifferenceInMs / (1000 * 60 * 60 * 24));
var daysUntilExpired = dateDifferenceInDays.toString().split('.');
// Get the expiration date and convert it to MM-DD-YYYY
var expireDate = expiration_date.getDate();
var gd = new GlideDate();
gd.setValue(expireDate.toString());
// Change the text based on the calculation above
var message;
if (daysUntilExpired >= 0) {
message= "Records show your software license is due to expire on " + expireDate.getByFormat("MM-dd-yyy") + ".";
} else {
message= "Records show your software license has expired on " + expireDate.getByFormat("MM-dd-yyy") + ".";
}
template.print(message);