Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

getting datetime field in client script returns wrong time

Nate23
Mega Guru

Hello Developers,

I have run into an issue and have found a solution, but was wondering if there was something I was missing.... an easier way?

Issue: I have a reference variable on a catalog item to the user table. when someone selects a user I have an onchange script that gets that reference with a callback function to get a custom date/time field. The issue is that when I view it on my profile on the user record it displays the time in my timezone. when I pull it with this script it pulls it in the systems timezone. So it may be 4 hours off due to time differences.

client script:

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

var caller = g_form.getReference('hiring_manager', doAlert); // doAlert is our callback function

}

function doAlert(caller) { //reference is passed into callback as first arguments

try{

//alert(caller.u_test.getDisplayValue());

var ajax = new GlideAjax('ClientDateTimeUtils');

ajax.addParam('sysparm_name', 'setTimeZone');

ajax.addParam('sysparm_dt', caller.u_name_of_date_field_on_user);

ajax.getXML(doSomething);

}catch(e){

alert(e);

}

}

function doSomething(response){

var answer = response.responseXML.documentElement.getAttribute("answer");

g_form.setValue('name of time variable',answer);

}

Current solution:

I have a glideajax call in my onchange client script to a script include that simply returns the date by :

setTimeZone: function(){

var date = this.getParameter('sysparm_dt');

var dt = new GlideDateTime(date);

return dt.getDisplayValue();            

}

this works but I cant help but feel there is an easier way?

If you have any definitive knowledge on this, I will be very grateful.

Thanks,

Nate

6 REPLIES 6

if getreference is used in a client script it is best practice to use a callback function which i am using...



find_real_file.png


my script is bringing in about 10 fields from the user record which is why I am using get reference and its the initial way i set this up. I had to implement the glide ajax call to get the time/date in the users timezone format. which is the only reason i am using glideajax otherwise I wouldn't use either.


Just ran into this exact issue and was able to fix it with your comment, thank you!