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

SanjivMeher
Kilo Patron
Kilo Patron

Hi Zachary,



Please check below thread. You need to use onChange script and run a GlideAjax from there.



auto populate current date



Please mark this response as correct or helpful if it assisted you with your question.

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.

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.Capture.PNG


johnram
ServiceNow Employee
ServiceNow Employee

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