Dynamically change the date text format with email script

Nestor Padilla1
Giga Expert

Hi Experts

I'm trying to convert the date from a custom table with the email script so it shows either "Today" or something like Tue, June 02 2022.

For that, I am trying to use an email script:

I can get the value from the table with 

current.field_name();

But, when it comes to convert and compare, it does not works, so I tried with a query

Here are my two approach.

Please note, it is the first time I work with email scripts, so I'm not sure of what I am doing wrong:

BTW this is an scoped application, the notification email script is also created in the scoped application.

With GlideRecord:

(function runMailScript(current, template, email, email_action, event) {

    // Add your code here

var proclamation = new GlideRecord('x_nuvo_eam_facilities_work_orders');
	proclamation.addQuery("sys_id", current.sys_id);
	proclamation.query();
	
	if(proclamation.next()) {
    var today = new GlideDate().getByFormat('MM-dd-yyyy');
    var startDate = req.planned_start.getByFormat('MM-dd-yyyy');

    if (startDate == today) {
        template.print("Today");
    } else { 
    template.print(proclamation.planned_start.getByFormat('EEEE, MMMM dd'));
}
		
}
	})(current, template, email, email_action, event);

The output I am expecting is something like:

Flags Half-Staff Today through Sunday, May 30 (if the start date is today)

or

Flags Half-Staff Saturday, May 29 through Sunday, May 30 (if the start date is not today).

To test my scripting, I used script background and it converted the dates OK:

var req = new GlideRecord('x_nuvo_eam_facilities_work_orders');
req.addQuery('number', 'FWKD0014998');
req.query();
if(req.next()) {
    var today = new GlideDate().getByFormat('MM-dd-yyyy');
    var startDate = req.planned_start.getByFormat('MM-dd-yyyy');

    if (startDate == today) {
        gs.print('Flags Half-Staff Today through ' +  req.planned_end.getByFormat('EEEE, MMMM dd'));
    } else { 
    gs.print('Flags Half-Staff ' + req.planned_start.getByFormat('EEEE, MMMM dd') + ' through ' + req.planned_end.getByFormat('EEEE, MMMM dd'));
}
}
gs.print('Today is ' + new GlideDate().getByFormat('MM-dd-yyyy'));
gs.print('Start date is ' + req.planned_start.getByFormat('MM-dd-yyyy'));

 

The other approach I tried was using current.variable_name but not sure how much I can do with it.

var startDate = current.planned_start;
var startDateSNFormat = startDate.getByFormat("MM-dd-yyyy");
var currentDate = new GlideDate().getByFormat("MM-dd-yyyy");
var startDateDayFormat = startDate.getByFormat('EEEE, MMMM dd');
	
if (startDateSNFormat == currentDate) {
 		template.print('Today');
 	} else {
 		template.print(startDateDayFormat);
 	}

Yet, none of the above works in the email script.

If I user template.print(current.planned_start); I something like 2022-06-15 00:00:00 in the email notification, so I can get the value, but I can't convert it or use conditions IF.

Any help would be pretty much appreciated.

1 ACCEPTED SOLUTION

Nestor Padilla1
Giga Expert

I found the solution.

The problem is that the value coming from the table was received as a string, considering that is a Scoped application, I resolved by converting the string into a GlideDate object to be able to convert it.

Here is the final script:

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
		/* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
		/* Optional GlideRecord */ event) {
	
	var todayDate = new GlideDate().getByFormat('EEEE, MMM d');
	var startDate = new GlideDate();
	startDate.setValue(current.planned_start);
	var dateString = startDate.getByFormat('EEEE, MMM d'); // e.g. Friday June 3
	if (todayDate == dateString) {
	template.print("Today");
		} else {
			template.print(dateString);
		}

})(current, template, email, email_action, event);

View solution in original post

6 REPLIES 6

Yes. I'm running it on global scope.

The world works with ServiceNow

Nestor Padilla1
Giga Expert

I found the solution.

The problem is that the value coming from the table was received as a string, considering that is a Scoped application, I resolved by converting the string into a GlideDate object to be able to convert it.

Here is the final script:

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
		/* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
		/* Optional GlideRecord */ event) {
	
	var todayDate = new GlideDate().getByFormat('EEEE, MMM d');
	var startDate = new GlideDate();
	startDate.setValue(current.planned_start);
	var dateString = startDate.getByFormat('EEEE, MMM d'); // e.g. Friday June 3
	if (todayDate == dateString) {
	template.print("Today");
		} else {
			template.print(dateString);
		}

})(current, template, email, email_action, event);