g_form.setValue is not setting value in Date/time field for Onsubmit client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-23-2017 01:31 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-23-2017 01:42 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-23-2017 02:01 AM
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();
}
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-23-2017 03:31 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-23-2017 03:36 AM
Hi Simunek,
Yes i used getXmlWait(). I tried with business rule but it was not working therefore i tried to use client script...