Client script OnChange and javascript:gs.nowDateTime()

perl95110
Kilo Explorer

I have created 2 fields ( u_flag, u_flag_date) in the incident form
u_flag is type "True/False"
and
u_flag_date is of type "glide_date_time".
I am try set the 'u_flag_date' with the current date-time if 'u_flag' is checked.
My client script below is not working. Can anyone tell me what I am doing wrong?


function onChange(control, oldValue, newValue)
{
if (oldValue == newValue)
{
return;
}
else
{
//var endDateTime = gs.nowDateTime();
var endDateTime = new GlideDateTime(gs.nowDateTime());
g_form.setValue('u_flag_date',endDateTime);
}
}

11 REPLIES 11

Jay_Ford
Kilo Guru

The issue is that you don't have access to gs in client scripts so you will have to use a AJAX call. See the link below.

http://wiki.service-now.com/index.php?title=Set_Current_DateTime_In_Field


I get a NULL value when I use the ajax call example in the wiki.
Any ideas?


You could also try to create a global business rule and call it something like getDtCurrent. Create a function in the BR that returns the current date time.



function getDtCurrent(){
var dtcurrent = gs.nowDateTime();
return dtcurrent;
}


Then call this BR from the client script with an Ajax call like this.



function onChange(control, oldValue, newValue, isLoading) {

if (newValue != oldValue) {
return;
}
else{
var script = "getDtCurrent()";
var answer = AJAXEvaluateSynchronously(script);
g_form.setValue('u_flag_date', answer);
}
}


Thank you Jay but it is still giving me a "null" value.
Would it be easier if I changed the 'u_flag_date' type from glide_date_time to datetime?