I want to set today's date field automatically , when i opened the new form and same as with time so i have created two seperate fields for date and time those are need to be filled automatically when i open the new form could any one please help me.

suryapulletikur
Kilo Contributor

I want to set today's date field automatically , when i opened the new form and same as with time so i have created two seperate fields for date and time those are need to be filled automatically when i open the new form could any one please help me....?

1 ACCEPTED SOLUTION

rambo1
Tera Guru

If you want to use current time and date on different tables, the use this script include:

var MyDateTimeAjax = Class.create(); 
MyDateTimeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor,
{
nowDateTime: function ()
{
return gs.nowDateTime(); }
});

call this S.I in onload client script:
var ajax = new GlideAjax('MyDateTimeAjax'); 
ajax.addParam('sysparm_name', 'nowDateTime');
ajax.getXML(function ()
{
g_form.setValue('put your field name here', ajax.getAnswer());
});

View solution in original post

26 REPLIES 26

Hi Surya,



The point of the default value is to take that value when you create a new record. Just like I can default the caller on an incident to be "me", when you open it up, it will be your name that appears in that field. That's the purpose of the default value.



If you open a new record on your custom table (or whatever it is we were talking about at the top of this conversation) the default value will put in today's date for the default value on the date field if you use the line of code I indicated. Tomorrow it will show tomorrow's date.



Once the record is saved, the value stays locked where it was when the form was created (unless someone manually changes it.)



I hope that makes sense.


Ya I have understood but it's not at all working ....?


Is this a variable on a catalog item or a field on a table?


ya i mentioned in above right, It is a field in table and ofcourse i have wrote server side script but it 's not working can you suggest script please


I was able to set a separate date and time field with a display business rule and client script like this:



Business Rule


(function executeRule(current, previous /*null when async*/) {



        g_scratchpad.start_date = new GlideDate().getDisplayValue();


        g_scratchpad.start_time = new GlideTime().getDisplayValue();



})(current, previous);



Client script:



function onLoad() {


        if (g_form.getValue('start_date') == '') {


                  g_form.setValue('start_date', g_scratchpad.start_date);


        }



        if (g_form.getValue('start_time') == '00:00:00') {


                  g_form.setValue('start_time', g_scratchpad.start_time);


        }


}