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.

How to convert DateTime based on Requester's TimeZone

Pooja Magadum1
Tera Expert

Hello,

I want to convert DateTime value which would be based on requester's TimeZone.

If my TimeZone is IST and requester's TimeZone is 'America/New York' then I should see 'America/New York' time in RITM variable.

I have written a code like below:

OnLoad Client Script on RITM table :

function onLoad() {
var dateTime = g_form.getValue('variables.access_start').toString();
var getSysID = g_form.getUniqueValue();

var gaGetTimeZone = new GlideAjax('ShowDateTimeOfRequester');
gaGetTimeZone.addParam('sysparm_name','updateDateTime');
gaGetTimeZone.addParam('ritm_sys_id',getSysID);
gaGetTimeZone.addParam('selected_date',dateTime);
gaGetTimeZone.getXML(setDateTimeOfRequester);

function setDateTimeOfRequester(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert('Answer return ' +answer);
g_form.setValue('access_start',answer);
}
}


Script Include :

var ShowDateTimeOfRequester = Class.create();
ShowDateTimeOfRequester.prototype = Object.extendsObject(AbstractAjaxProcessor, {

updateDateTime: function() {
var requester, time ;
var selectedDate = this.getParameter('selected_date');
var ritmSysID = this.getParameter('ritm_sys_id');
var grReqItem = new GlideRecord('sc_req_item');
grReqItem.addQuery('sys_id',ritmSysID);
grReqItem.query();
if(grReqItem.next()) {
requester = grReqItem.request.requested_for;
}
var user = new GlideRecord('sys_user');
user.query('sys_id',requester);
user.query();
if(user.next()) {
time = new GlideDateTime(selectedDate);
gs.addInfoMessage('First Time ' +time);
var tz = Packages.java.util.TimeZone.getTimeZone(user.time_zone);
time.setTZ(tz);
gs.addInfoMessage('Requester Time is ' +time);

gs.addInfoMessage('Requester Display Time is ' +time.getDisplayValue());
}
return time;
},
type: 'ShowDateTimeOfRequester'
});


But its not working 😞
Please provide any solution.
Awaiting for your response.

--
Thanks,
Pooja

1 ACCEPTED SOLUTION

Shashi Bhushan
Kilo Guru

https://community.servicenow.com/community?id=community_question&sys_id=b28a4fe9db5cdbc01dcaf3231f96192b

 

 

Thanks

Please mark correct/helpful.

View solution in original post

5 REPLIES 5

Pooja Magadum1
Tera Expert

That code helped.

 

Now its working as I have made below changes in code :

 

Script Include:

var ShowDateTimeOfRequester = Class.create();
ShowDateTimeOfRequester.prototype = Object.extendsObject(AbstractAjaxProcessor, {

updateDateTime: function() {

var requester, time;
var selectedDate = this.getParameter('selected_date');
var ritmSysID = this.getParameter('ritm_sys_id');
var grReqItem = new GlideRecord('sc_req_item');
grReqItem.addQuery('sys_id',ritmSysID);
grReqItem.query();
if(grReqItem.next()) {
requester = grReqItem.request.requested_for;
}
var user = new GlideRecord('sys_user');
user.query('sys_id',requester);
user.query();
if(user.next()) {

time = new GlideDateTime(selectedDate);
gs.addInfoMessage('First Time ' +time);
var tz = Packages.java.util.TimeZone.getTimeZone(user.time_zone);
time.setTZ(tz);
time.setValue(grReqItem.variables.access_start);
gs.addInfoMessage('1 Requester Variable Time is ' +grReqItem.variables.access_start);
gs.addInfoMessage('2 Requester Time is ' +time);
gs.addInfoMessage('3 Requester Display Time is ' +time.getDisplayValue());
}
return time.getDisplayValue();

},
type: 'ShowDateTimeOfRequester'
});