Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Email Script Needs to show current year

pramn
Kilo Sage

Team , 

we have a notification which should show current year but it's showing 2022, i checked there is mail script running , can you help me , how it can be changed to current year (2023) 

pramn_0-1679467341416.png

Script 

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

// Add your code here
var fiscalYear = current.u_year || current.u_actual_assignment.u_year || current.u_parent.u_year;
var fisCalYearArr = fiscalYear.split('-');
template.print(fisCalYearArr[1]);

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

 

 

 

5 REPLIES 5

Kalyani Jangam1
Mega Sage

Hi @pramn 

var gdt = new GlideDateTime(current.u_year); //current.u_actual_assignment.u_year || current.u_parent.u_year

gs.info(gdt.getYearUTC());

simple way to calculate year for particular field value.

 

Please check and Mark Helpful or Correct if it really help you.

Now the year not showing 

pramn_0-1679468486679.png

 

can you please confirm u_year is date field then only it will calculate year in GlideDateTime???

Dhruv Chandan
Giga Guru

Hi ,

 

To get the current year you may use the below code :-

 

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

template.print(new Date().getFullYear());

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

 

Hope this helps as this will definitely print the current year automatically even in the future.

 

Regards,
Dhruv