g_form.setValue is not setting value in Date/time field for Onsubmit client script

peshwen
Giga Contributor

Hi All,

I have created one date/time field 'Date' which is read only. I have written one client script to populate the field with current date and time. For it i have used script include which will return current time using gs.nowDateTime(). I am using this because i want the Date field should be populated even if i hit 'Save' button on form and no changes are done to other fields on form.

While saving it is showing that field is populated with current date time...but after record is saved and form is loaded..Date field is blank..

Please help me   why it is happening like that...i want to save date field with date and time when i just hit 'save or update' button and make no changes to fields on form\

Thanks,

Nikhil

9 REPLIES 9

Alikutty A
Tera Sage

Hi,



Can you please show the script which you have written for the date field and on what trigger is it populated every time?



Also Why dint you consider the Updated field for this requirement?



Thanks


Please Hit like, Helpful or Correct depending on the impact of the response


nayanawadhiya1
Kilo Sage

Hey Nikhil,



This is the Client Script :



function onSubmit() {  



        //Type appropriate comment here, and begin script below  


                  var ajax = new GlideAjax('MyDateTimeAjax');  


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


                ajax.getXML(function () {  


                        g_form.setValue('Your_field', ajax.getAnswer());


                            g_form.save();


      });


}


  Define the Script Include MyDateTimeAjax. Go to Script Includes -> Create new Name : MyDateTimeAjaxClient Callable : true ( check it) Code :



var MyDateTimeAjax = Class.create();  


MyDateTimeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {


      nowDateTime: function () {  


return gs.nowDateTime();  


      }


});


Shouldn't it be "getXMLWait" as you are in onSubmit client script so that call is synchronous? Can you debug if the form is really sent to server for update? If you did not change anything on the form, it is possible that no update is actually done on server. You might want to create a before update business rule and place a gs.log there to check if it is even executed and if the value is delivered to server.


Hi Simunek,



Yes i used getXmlWait(). I tried with business rule but it was not working therefore i tried to use client script...