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

Danish Bhairag2
Tera Sage
Tera Sage

Hi @Jessica28 ,

 

It seems like there's a small syntax error in your code. You're missing a closing quote in the 'Date is invalid' string, and there's a typo in the 'template.print' function. Here's the corrected code:

 

 

if (current.variables.startDate.nil()) {

    template.print('Date is invalid');

} else {

    template.print('Date is valid');

}

 

 

Make sure to close the quote properly in 'Date is invalid'. Also, ensure that you have a closing parenthesis for the `template.print` function.

 

If this still doesn't work, you may want to check if the variable name 'startDate' is correct and matches the actual variable name on your RITM form. Additionally, consider checking if there are any JavaScript errors in the ServiceNow logs that might provide more insights into the issue.

 

Thanks,

Danish

 

Hi @Jessica28 ,

 

You can also try something like this

 

if (current.variables.startDate == '') {

    template.print('Date is invalid');

} else {

    template.print('Date is valid');

}

 

Thanks,

Danish

 

Hello @Danish Bhairag2 

I tried using a different date that I know for sure that this date variable is empty, but it is still printing "Date is valid'.  I really don't where I screwed up.

Jessica28_0-1702705255734.png

 

 

@Jessica28 

 

From where is the email script getting called?

Hope it's from the notification which is created on RITM table or some table where the variable is present.

 

Thanks,

Danish