The CreatorCon Call for Content is officially open! Get started here.

auto populate current date

SD29
Tera Expert

Hi All,

I have a requirement where i have to auto populate a date field based on the field ('information' - field type is checkbox). If the checkbox is checked then populate today date in the date field.

How can i achieve this, please help.

Thanks in Advance,

SD.

1 ACCEPTED SOLUTION

Shishir Srivast
Mega Sage

Please check this as well.



Name: Set Current Date/Time In Field


Type: Client Script


Table:


Description: You can use the following two lines to set the current date and time in a date/time field. This bypasses the problem of getting the value into the proper format and proper timezone:


Parameters:


Script: For more information on running Server Side Script with the client please refer to GlideAjax.


var ajax = new GlideAjax('MyDateTimeAjax'); 
ajax.addParam('sysparm_name', 'nowDateTime');
ajax.getXML(function () { g_form.setValue('put your field name here', ajax.getAnswer()); });

System Script Include

// Be sure the "Client callable" checkbox is checked   var MyDateTimeAjax = Class.create(); 
MyDateTimeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, { nowDateTime: function () { return gs.nowDateTime(); }

View solution in original post

9 REPLIES 9

Shishir Srivast
Mega Sage

Please check this as well.



Name: Set Current Date/Time In Field


Type: Client Script


Table:


Description: You can use the following two lines to set the current date and time in a date/time field. This bypasses the problem of getting the value into the proper format and proper timezone:


Parameters:


Script: For more information on running Server Side Script with the client please refer to GlideAjax.


var ajax = new GlideAjax('MyDateTimeAjax'); 
ajax.addParam('sysparm_name', 'nowDateTime');
ajax.getXML(function () { g_form.setValue('put your field name here', ajax.getAnswer()); });

System Script Include

// Be sure the "Client callable" checkbox is checked   var MyDateTimeAjax = Class.create(); 
MyDateTimeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, { nowDateTime: function () { return gs.nowDateTime(); }

Saikiran Gudur1
Mega Guru

Hi snow123@,



Please check this http://wiki.servicenow.com/index.php?title=Set_Current_DateTime_In_Field#gsc.tab=0



If you found this helpful, please mark it as "helpful/Correct Answer" Or Like it.



Regards,


Saikiran Guduri


balaji_charapal
Kilo Guru

On client side only you can get today date with below script, check if it is useful.


var dFormat = g_user_date_format;

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


          g_form.setValue('todayDate', tdayDate);



write this 3 lines of code in onChange client script.


@balaji.charapalli@lntinfotech :it works but i didnt understand the logic, can you please explain the logic.

can anyone help me for understanding the logic.

Go through the below article

https://servicenowguru.com/scripting/client-scripts-scripting/client-side-dates-in-servicenow/