The CreatorCon Call for Content is officially open! Get started here.

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

Sagar Pagar
Tera Patron

Hi @Nestor Padilla,

 

Try this script in your email script.

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
		/* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
		/* Optional GlideRecord */ event) {
	
	var today = new GlideDate().getByFormat('MM-dd-yyyy');
	var startDate = current.planned_start.getByFormat('MM-dd-yyyy');

	if (startDate == today) {
		template.print("Today is " + new GlideDate().getByFormat('EEE, MMMM dd yyyy')); 
	} else { 
		template.print("Start date is " + current.planned_start.getByFormat('EEE, MMMM dd yyyy'));
	}

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

 

Thanks,
Sagar Pagar

The world works with ServiceNow

Hi @Sagar Pagar 

Thanks a lot for your response. I tried it but still didn't work.

As soon as I try to convert the date-format it stop displaying in the notirication.

I can get current.planned_start to work OK but, if I use any .getFormat... automattically stops showing up.

So, I guess that converting it directly would not work. Hence, I think I should query the table and get the values and then convert it but, as you can see above, either that doesn't work either or I am not building the script accurately.

Nestor

Hi,

For me it is showing the correct results. I used the same scripts.

For Today:

find_real_file.png

 

 

For Other days:

find_real_file.png

 

Thanks,
Sagar Pagar

The world works with ServiceNow

Could it be that you are running on global? this is an scoped application. I think that that is the issue. NOt 100% sure though.