- 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
‎07-24-2017 09:52 AM
Hi Zachary,
Please check below thread. You need to use onChange script and run a GlideAjax from there.
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
‎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
‎07-24-2017 10:30 AM
Thank you for your reply. Below is a screen shot of my client script. I was able to get it working! The system script include has to be named the same name as the GlideAjax name above ('MyDateTimeAjax'), once I did that it worked. Just thought I'd mention that for anyone else trying to do this.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-19-2017 08:45 AM
The ServiceNow Wiki content is no longer supported. Updated information about this topic is located here: Date and Time Fields
Visit http://docs.servicenow.com for the latest product documentation