isInSchedule isn't returning true in a specific timezone. Any thoughts?

Tej2
Giga Expert

Hi folks,

I'm trying to do a fairly simple thing. To check if a selected date falls under a defined schedule. The date is available to the user on the form.

I'm validating the date with onChange client script and passing this value to the Script Include, to check if the date exists in the said schedule.

The schedule is defined in US/Pacific TZ and is set to repeat weekly Mon - Fri between 9am and 5pm.

Instance: Jakarta

When the SI function is invoked from the client script, the response is false for the time between 9am and 5pm Pacific, but returns true for some other random time, which I'm guessing it to be a random time-zone.

The strange thing is, if I call similar function from a BR during insert, then it works absolutely fine. The challenge is when I call from client script.

I also checked using background script and seems to work fine.

I'm not sure what I'm doing wrong here. Appreciate your help.

I've tried both the methods below:

Script Include:

Method - 1:

var tz = Packages.java.util.TimeZone.getTimeZone('US/Pacific');

var input = this.getParameter('sysparm_input'); // THIS IS THE INPUT DATE FROM CLIENT SCRIPT

//var curr_date = new GlideDateTime();

var sched = new GlideSchedule('31b61ed2dbbb0b00c46ae5b74b9619ef', 'US/Pacific');

var convertInputToGDT = new GlideDateTime();

convertInputToGDT.setTZ(tz); // SETTING US/Pacific TIME ZONE

convertInputToGDT.setValue(input);// CONVERTING THIS DATE TO GlideDateTime

var inSchedule = sched.isInSchedule(convertInputToGDT); // CHECKING IF IT EXISTS IN SCHEDULE

if(inSchedule) {

return 'true';

}

else {

return 'false';

}

Method - 2:

var sched = new GlideSchedule('31b61ed2dbbb0b00c46ae5b74b9619ef', 'US/Pacific');

var convertInputToGDT = new GlideDateTime(input);

var inSchedule = sched.isInSchedule(convertInputToGDT);

//var inSchedule = sched.isInSchedule(convertInputToGDT, 'US/Pacific'); // <-- have tried this too

if(inSchedule) {

return 'true';

}

else {

return 'false';

}

Client Script:

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

if (isLoading || newValue === '') {

return;

}

var gaSchedule = new GlideAjax('ScriptIncludeName');

gaSchedule.addParam('sysparm_name','checkSchedule');

gaSchedule.addParam('sysparm_input', g_form.getValue('custom_date'));//actual date should be passed here

gaSchedule.getXML(populateResponse);

}

function populateResponse(response) {

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

if(answer == 'true') { // have also tried using ===

g_form.showErrorBox("custom_date", 'is in schedule');

}

}

1 ACCEPTED SOLUTION

Jon Barnes
Kilo Sage

I believe the reason for this is because the value you get from the client should be the user's time zone (and in the user's date/time format), so you would want to use setDisplayValue on your datetime object. and you shouldn't need to set the TZ on the date time object for it to work. Also, if the schedule is already in US/Pacific, you don't need to pass that into the schedule api.



So, in your script include, this should work:



var input = this.getParameter('sysparm_input'); // THIS IS THE INPUT DATE FROM CLIENT SCRIPT  


var sched = new GlideSchedule('31b61ed2dbbb0b00c46ae5b74b9619ef');  


var convertInputToGDT = new GlideDateTime();    


convertInputToGDT.setDisplayValue(input);// CONVERTING THIS DATE TO GlideDateTime  


return sched.isInSchedule(convertInputToGDT); // CHECKING IF IT EXISTS IN SCHEDULE


View solution in original post

4 REPLIES 4

Jon Barnes
Kilo Sage

I believe the reason for this is because the value you get from the client should be the user's time zone (and in the user's date/time format), so you would want to use setDisplayValue on your datetime object. and you shouldn't need to set the TZ on the date time object for it to work. Also, if the schedule is already in US/Pacific, you don't need to pass that into the schedule api.



So, in your script include, this should work:



var input = this.getParameter('sysparm_input'); // THIS IS THE INPUT DATE FROM CLIENT SCRIPT  


var sched = new GlideSchedule('31b61ed2dbbb0b00c46ae5b74b9619ef');  


var convertInputToGDT = new GlideDateTime();    


convertInputToGDT.setDisplayValue(input);// CONVERTING THIS DATE TO GlideDateTime  


return sched.isInSchedule(convertInputToGDT); // CHECKING IF IT EXISTS IN SCHEDULE


Thanks Jon for taking a look into this.



Setting the input via setDisplayValue(), worked like a charm.



Not sure, why I didn't think of this before



The users BTW, will all be based out of US/Pacific timezone.


it is always returning FALSE even i taken Business Days.

in Schedule, user profile both are had same TIMEZONE's that is GMT

Kunal Jha
Giga Expert

Tej,



I assume this is the cause:


when date is getting stored in database its in GMT but when we select it on the form it show in user's specific timezone PST



I filled Planned start date and tried to print using BR and client script:



Client Script output:


find_real_file.png



BR Output:



find_real_file.png



There is 8 hour difference in timezone so with 8 hours of difference you should get same result.



Hope this will solve your issue.