
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-28-2016 02:58 PM
Trying to do some population of form field in catalog item via client script - pretty simple, just doing a GlideRecord lookup to the change_request table and pulling down the planned start date which I set in a field. Issue is the value being returned from the GlideRecord is UTC and I need to display it in local time. So far I haven't found a way to make the conversion. Any one cracked this nut before?
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var gr = new GlideRecord('change_request');
gr.addQuery('sys_id', newValue);
gr.query();
if (gr.next()) {
g_form.setValue('deploy_date_time', gr.start_date);
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-29-2016 11:08 AM
No worries! Here you go...
I designed this so you can reuse this script for other date fields in the change in case you ever need to. What I mean there you will notice in the client script I am passing the AJAX Script Include "start_date" so it knows what field to convert. If you ever have a need to translate other change dates like end_date this would still work.
Change your client script to the following:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '')
return;
var ajax = new GlideAjax('LocalChangeDateTimeAjax');
ajax.addParam('sysparm_name', 'getLocalTime');
ajax.addParam('sysparm_dateField', 'start_date');
ajax.addParam('sysparm_changeNumber', newValue);
ajax.getXML(function () {
g_form.setValue('deploy_date_time', ajax.getAnswer());
});
}
Then navigate to System Definition\Script Includes and click New.
1. In Name enter: LocalChangeDateTimeAjax
2. Check the Client Callable checkbox - VERY important
3. In script, replace what's there with:
var LocalChangeDateTimeAjax = Class.create();
LocalChangeDateTimeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor , {
getLocalTime: function() {
var dateField = this.getParameter('sysparm_dateField');
var changeID = this.getParameter('sysparm_changeNumber');
var changeRec = new GlideRecord("change_request");
changeRec.get(changeID);
var dateValue = changeRec[dateField].getDisplayValue();
return dateValue;
},
type: 'LocalChangeDateTimeAjax'
});
4. Click Submit.
5. Give it a test!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-29-2016 12:22 PM
This worked great! Really appreciate the help. I acknowledge the advice to avoid GlideRecord in client side. Typical scenario here, got pulled into an effort that was struggling to get off the ground and having to consume Javascript and ServiceNow on the go. Lots to learn and not enough time. Read the advice but figured I'd fix it in the next release or even better the next guy would.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-29-2016 12:57 PM
Michael,
Thanks for posting this. I'm just curious, as I have seen this in a couple of different examples. In the wiki documentation, the example GlideAjax client script (and the one I have been using) is something like:
ajax.getXML(HelloWorldParse);
function HelloWorldParse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
}
And, I see here you've done:
- ajax.getXML(function () {
- g_form.setValue('deploy_date_time', ajax.getAnswer());
- });
Other than a little bit less code, is there an advantage/disadvantage to using 'ajax.getAnswer()' rather than 'var answer = response.responseXML.documentElement.getAttribute("answer");'?
Is there a particular case where you would use one over the other?
Cheers,
Tim
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-29-2016 01:42 PM
They are actually doing the same thing and I have used both. Honestly just depends where I copy and paste my example code from! My version just has the function embedded in the getXML call.
The getXML() method works on both desktop and mobile, I could have also used getXMLWait() but that only works on desktop clients.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-29-2016 01:50 PM
Thanks Mike!
Cheers,
Tim
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-29-2016 03:26 PM
No need for an ajax call, just look at g_user_date_time_format
see my reply to this thread