- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-24-2017 09:48 AM
Hi, i'm a new developer and need to set a variable called u_date_completed to the current time when the state is equal to completed. I tried creating an onChange client script for the state field when it changes to complete to set the date value of u_date_completed. Any ideas on how to make that happen would be great. Attached is a screen shot to help.
Solved! Go to Solution.
- Labels:
-
Best Practices
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-24-2017 09:53 AM
You can also refer the below example provided by ServiceNow
Set Current DateTime In Field - ServiceNow Wiki
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(); } });
Please mark this response as correct or helpful if it assisted you with your question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-31-2018 07:13 AM
A small update to this
We ran into some problems with this being asynchronous and not replying as we expected.
The Server side of the script works just fine but we did some changes to the Client side.
var ajax = new GlideAjax('MyDateTimeAjax');
ajax.addParam('sysparm_name', 'nowDateTime');
ajax.getXMLWait();
g_form.setValue('your_fieldname goes here', ajax.getAnswer());