How do I set a date field when another field changes

Kaz9
Giga Expert

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.

1 ACCEPTED SOLUTION

SanjivMeher
Kilo Patron
Kilo Patron

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.

View solution in original post

5 REPLIES 5

Stewe Lundin
Mega Guru

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());