How to check date and time variable if it is empty or not using email script?

Jessica28
Tera Guru

Hello,

I have a date and time variable on the RITM that I need to check to see if it is empty or not.

I tried this code in email script, but it is not working:  Thank you

 

if (current.variables.startDate.nil()) {
template.print('Date is invalid);
} else {

template.print('Date is valid);

 

 

2 ACCEPTED SOLUTIONS

Jessica28
Tera Guru

Thank you so much for your help. I think I got it working now.

 

Jessica28_0-1702710862988.png

 

View solution in original post

Hi @Jessica28 
In mail script you already have the current object using which you can dot walk to RITM table, no need to do GlideRecord on RITM table.



Thanks and Regards,

Saurabh Gupta

View solution in original post

12 REPLIES 12

Ankur Bawiskar
Tera Patron
Tera Patron

@Jessica28 

I am not sure email notification is on which table and email script is on which table but here is the syntax to check empty

If your email notification and email script is on RITM table then this should work fine

if (current.variables.startDate == '') {
template.print('Date is invalid);
} else {
template.print('Date is valid);
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar 

The notification is on sysapproval_approver table and email script is on sc_req_item table:

This is the correct syntax?

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */
email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */
event) {

var ritmGlide = new GlideRecord('sc_req_item');
ritmGlide.addQuery('sys_id', current.document_id.toString());
ritmGlide.query();
if (ritmGlide.next()) {
var date = ritmGlide.variables.select_end_date_time.getDisplayValue();
template.print('Date is valid');
} else {
template.print('Date is invalid');
}

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

 

@Jessica28 ,

 

The above looks correct Jessica.

 

@Ankur Bawiskar , please provide your input as well.

 

Thanks,

Danish

 

Hi @Jessica28 

You can try as below.

 

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */
email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */
event) {
var date=current.sysapproval.variables.select_end_date_time.getDisplayValue();
var msg="Date is not valid"
if (date) 
{
msg='Date is valid';
} 
template.print(msg);


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

 

 


Thanks and Regards,

Saurabh Gupta

Hi @Jessica28 
In mail script you already have the current object using which you can dot walk to RITM table, no need to do GlideRecord on RITM table.



Thanks and Regards,

Saurabh Gupta