What is the "gs.nowDateTime" equivalent Client side?

peterraeves
Mega Guru

I want to fill out date fields automatically, when a form is loaded. Very straightforward, but now it seems I can't call gs.nowDateTime client-side. I have read through the API, but can't seem to find the client-side equivalent. Is there no equivalent, because I shouldn't be doing something like this through client scripts? Or did SN just forget about it, which I doubt.

1 ACCEPTED SOLUTION

mayurt
Tera Expert

Hi Peter,



Please go through the following link hopefully that will help you.


Client Script Date/Time Functions


View solution in original post

7 REPLIES 7

mayurt
Tera Expert

Hi Peter,



Please go through the following link hopefully that will help you.


Client Script Date/Time Functions


I tried the Ajax script provided by SN themselves (Set Current DateTime In Field - ServiceNow Wiki), but that doesn't work. The ajax script won't seem to return anything... I used the following code and the InfoMessages 'first' and 'second' both showed up, but the alert never came. Changing the alert with another InfoMessage didn't work either.



MyDateTimeAjax - Script Include:


var MyDateTimeAjax = Class.create();


MyDateTimeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {


      nowDateTime: function () {


              return gs.nowDateTime();


      }


});


Client Script on Change Request and onLoad:


function onLoad() {


  g_form.addInfoMessage('first');


  if (g_form.getValue('type') == 'ActionRequest') {


  g_form.addInfoMessage('second');


  //If we call gs.nowDateTime() three times, the values will slightly differ.


  //If we call gs.nowDateTime() only once and store it in a value, it will be the same for all three fields.


  var ajax = new GlideAjax('MyDateTimeAjax');


  ajax.addParam('sysparm_name', 'nowDateTime');


  ajax.getXML(function () {


  alert('test');


  var nowDateTime = ajax.getAnswer();


  g_form.setValue('requested_by_date', nowDateTime);


  g_form.setValue('start_date', nowDateTime);


  g_form.setValue('end_date', nowDateTime);


  });


  }


}


Edit: It seems like I'm getting the following error though "Script: MyDateTimeAjax not found in scope: global, and HTTP Processor class not found: com.glide.processors.xmlhttp.MyDateTimeAjax: no thrown error", despite them being in the global scope.


OK... It seems that the Script Include was de-activated and that's why it obviously couldn't be found in the global scope... After turning it on again, it works fine.



To conclude, after reading both of your comments. It seems that SN does not offer Date functionality client side and that you need to access it through AJAX calls. I didn't expect that, but good to know.