How to auto-populate date field on record producer

JordanDR
Tera Contributor

Hi Community,

We are creating a record producer on our Service Portal for hardware return in the office. The form is pretty straightforward and has been easy to make. One of the questions is the date of hardware return. Currently, it requires the user to click into the menu and choose the date. Is there any way to make it so that the date field auto-fills to the current date?

find_real_file.png

It is a small thing but I want to keep the number of user actions on this form to a minimum!

Thank you!

1 ACCEPTED SOLUTION

Aman Kumar S
Kilo Patron

Hey,

Just use this in default value of the variable javascript: new GlideDate();

Best Regards
Aman Kumar

View solution in original post

6 REPLIES 6

Logan Poynter
Mega Sage
Mega Sage

Hello name,

You can set a default value for the variable.

Use gs.getDateTimeNow(); if it's Date/Time

Use gs.getDateNow(); if it's Date


Please mark my answer as correct/helpful if it has helped you.

Thanks,
Logan
--
LinkedIn

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Jordan

From the screenshot, the field is of type Date. To set the default date to today, I usually set it to "now".

find_real_file.png

Execution result.

find_real_file.png

Hey, Just use this in default value of the variable javascript: new GlideDate();
Best Regards
Aman Kumar

My bad.

now is just a shorthand for gs.now(). This, however, would return date in UTC.

To get the date in user's timezone, use the following:

javascript: (new GlideDate()).getDisplayValue()

find_real_file.png

Timezone problem becomes apparent if the type is set to Date/Time.

Setting the default to "javascript: new GlideDateTime()" would result in the wrong time because ServiceNow's internal timezone is UTC.

Execution result:

case 1: using just "new GlideDateTime()". The time is in UTC.

find_real_file.png

case 2: using "(new GlideDateTime()).getDisplayValue(). Time is in end-user's timezone.

find_real_file.png

Likewise, it's is necessary to use (new GlideDate()).getDisplayValue() to get the correct date in end-user's timezone.