How do I display dynamic text based on today's date in a mail script

neil_b
Tera Guru

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."
1 ACCEPTED SOLUTION

neil_b
Tera Guru

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);

 

 

View solution in original post

4 REPLIES 4

Abhay Kumar1
Giga Sage

@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.

Thank you so much @Abhay Kumar1! I will try this and report back soon!

 @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.

var expirationDateFormatted = expirationDate.getByFormat("MM-dd-yyyy");
 
I think the bolded text is what's causing the issue and we may need a different method to change the date format.
 
 

neil_b
Tera Guru

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);