How to default a text box to display current year

anjiadmin
Tera Expert

How to default a text box to display current year.

Thanks,

Anji

11 REPLIES 11

ramireddy
Mega Guru

Add this line in UI page in client script section.



txtYear.value = new Date().getFullYear();


You want me to enter that code in default section of the field?


No, For table columns, you can't specify an expression, unless its calculated. As this field is not calculated column, we should do it on form.



Create a client script for this table -> Implement "Onload" event, in the script, write like below.




g_form.setValue('your field name here ',new Date().getFullYear());




Following link will help you to understand more about client scripts.



Client Scripts - ServiceNow Wiki


Actually, you can set the Default to the following code:


javascript:(new Date().getFullYear()).toString().substr(0,4);



I had to add the extra "substr" method because without it, the year ended up with an extra ".0" at the end of it for some reason.