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

I've got it now. I was missing the script include part


I am changing my client script a little bit. When the flag is checked (true) , put the date-time and when the flag is uncheck , clear the date field.
My script is not doing what I want. Anyone can give me a clue?


function onChange(control, oldValue, newValue)
{

if (oldValue == newValue)
{
return;
}
else
{
var ctime = g_form.getValue('u_flag');

alert(newValue);
if ( ctime == true)
{
var ajax = new GlideAjax('MyDateTimeAjax');
ajax.addParam('sysparm_name','nowDateTime');
ajax.getXMLWait();
var rightnow = ajax.getAnswer();
g_form.setValue('u_flag_date', rightnow);
}
else
{
alert ("Clear date field");
g_form.setValue('u_flag_date','');
}
}
}


CapaJC
ServiceNow Employee
ServiceNow Employee

Try the following, too (it would save an AJAX call if it works):

var endDateTime = new GwtDate().now().serializeInUserFormat(false);
g_form.setValue('u_flag_date', endDateTime);


I still get an error
'GwtDate' is undefined


Ivan Martez
ServiceNow Employee
ServiceNow Employee

I don't like using the getXMLWait method because it locks the browser until an answer is returned. I just tried this in my instance. I created 2 fields, the first field was a Yes/No variable, the second field was a date/time field. Here is the code.

Script Include -

Name: MyDateTimeAjax
Client Callable: true

Script:




var MyDateTimeAjax = Class.create();

MyDateTimeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {

nowDateTime: function() {
return gs.nowDateTime();
}
});


Client Script -
Name: set the date
Type: onChange
Applies to: Catalog Item

Script:


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

if(newValue == "Yes"){

var ga = new GlideAjax('MyDateTimeAjax');
ga.addParam('sysparm_name', 'nowDateTime');
ga.getXML(setDateTimeParse);
}

function setDateTimeParse(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('my_date_field', answer);

}
}