How to auto populate catalog item variable with current date

WP2
Kilo Guru

Hi ALL,

I have a variable with Date Type and I want the variable to auto populate current date. I used onChange client script with below script but no date populated. Could you please help with a script to achieve this requirement. Thank you.

var dFormat = g_user_date_format;

var tdayDate = formatDate(new Date(),dFormat);


g_form.setValue('todayDate', tdayDate);

1 ACCEPTED SOLUTION

Utpal Dutta1
Mega Guru

Hey Rich,

 

Go to variable dictionary (right click on the variable) and in default value write this: javascript:gs.nowDateTime()

 

 

If it works the please mark my answer Correct and Helpful.

 

 

Thanks and Regards:

Utpal Dutta 

View solution in original post

10 REPLIES 10

Hey Rich,

 

I'm glad that my solution worked for you. Try this client script for the requester title.

 

var caller = g_form.getReference('caller_id', doAlert);

function doAlert(caller) {

g_form.setValue(your field name, caller.title);


}


Please mark my answer Correct and Helpful.

 

 

Thanks and Regards:

Utpal Dutta

No luck so far. I am guessing i need to change maybe 'caller" to the variable name?

Hi Rich,

just to inform as you have mentioned the variable type is Date so this won't work as it would return date/time

javascript:gs.nowDateTime()

Also the function nowDateTime is not allowed in scope; so please refer below

For Date/Time Variable Default

javascript: new GlideDateTime();

For Date Variable Default to today

javascript: new GlideDateTime().getDate()

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

Correct

Mark Roethof
Tera Patron
Tera Patron

Hi there,

For a variable, you can just add a default value. Looking at your description, I don't see a reason for using a (Catalog) Client Script.

Some examples:
javascript:new GlideDate();
javascript:new GlideDateTime();
javascript:gs.nowDateTime();

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn