How to fix NaN error?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-28-2018 12:18 PM
Hey guys, I have a script include as follows:
var ourcompanyClientDateTimeUtils = Class.create();
ourcompanyClientDateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
withinBusinessHours: function() {
var retVal; // Return value
var pn = this.getParameter('sysparm_hours');
var requestedDate= this.getParameter('sysparm_requestedDate');
var reqDateTime = new GlideDateTime(requestedDate);
gs.log(reqDateTime);
if(pn > 0){
//Get a schedule by sys_id to calculate duration
var schedule = new GlideSchedule();
//schedule.load('090eecae0a0a0b260077e1dfa71da828');
schedule.load('08fcd0830a0a0b2600079f56b1adb9ae');
//Get the current date/time in correct format for duration calculation
var actualDateTime = new GlideDateTime();
actualDateTime.setDisplayValue(gs.nowDateTime());
//Create variable to capture the date we want to compare against
//requestedDate = new GlideDateTime("2017-06-16 00:00:00");
//Date difference calculated based on specified schedule
var difDay = schedule.duration(actualDateTime,reqDateTime).getDayPart()*24;
var difHour = schedule.duration(actualDateTime,reqDateTime).getDurationValue().split(':')[0].substr(-2);
var dif = difDay + parseInt(difHour.replace(/^[0]+/g,""));
gs.log("SMRZ - " + dif + "," + pn + "," + difDay + "," + difHour );
if(dif >= pn){
retVal = "True";
} else {
retVal = "False";
}
gs.log(retVal);
return retVal;
}
},
type: 'ourcompanyClientDateTimeUtils'
});
And I have a client script as follows:
function onChange(control, oldValue, newValue, isLoading) {
g_form.hideFieldMsg('needed_by');
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
/************* This Checks whether the selected date is within a week **********/
//alert(newValue);
var ga = new GlideAjax('ourcompanyClientDateTimeUtils');
ga.addParam ('sysparm_name','withinBusinessHours');
ga.addParam ('sysparm_hours','45');
ga.addParam ('sysparm_requestedDate',newValue);
ga.getXML(CheckScheduleAgainstDatesParse);
}
function CheckScheduleAgainstDatesParse (response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer == 'False'){
g_form.showFieldMsg('needed_by','Please allow at least a week','error');
g_form.clearValue('needed_by');
}
}
The function is simple. I have a few dates in a form (catalog item) and I do not want the user to be able to select dates that are within a week of the current date. Currently, the form works for the dates that have passed or are within a week of the current date, however, it fails for some of the dates that are way ahead, like 08/09/2018 and 08/21/2018 and a few more. If a date that is within a week or has passed, the value should return to --None-- and the user should have to select again, however, when 08/09/2018 and 08/21/2018 and a few others are selected, the value changes to --None-- when it shouldn't.
I created a log in my script include as you can see. It is:
gs.log("SMRZ - " + dif + "," + pn + "," + difDay + "," + difHour );
For a date that has passed or is within a week, I get this log: SMRZ - NaN,45,0,00
However, for a date that is beyond a week and is not working, I get this log: NaN,45,72,00 (ofc the numbers vary depending on date but the NaN is still there)
For a date that is working perfectly fine, I get this log: 882,45,864,18
Please help me with this issue! Any urgent help will be greatly appreciated. I need to complete this in a few days!
Thank you!
- Labels:
-
Service Catalog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2018 10:38 AM
Also, when I log the values,
These are the results.
The results are in the order === dif, pn, difDay, difHour
For values that do NOT AND should not work: NaN,45,0,00
For values that SHOULD work BUT do not work: NaN,45,336,00
For values that SHOULD work and work: 246,45,240,06 etc
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2018 09:25 AM
Hi,
I think you have complicated the whole thing. if the requirement is " I do not want the user to be able to select dates that are within a week of the current date "-- that means, 7 days to adm fro it cannot be set.
ill focus only on that part then:
in the script include ---
var retVal; // Return value
var pn = this.getParameter('sysparm_hours');
var requestedDate= this.getParameter('sysparm_requestedDate');
var reqDate = new GlideDate(requestedDate);// date
gs.log(reqDateTime);
var actualDate = new GlideDate();
if(reqDate >actualDate.addDays(-7) && reqDate <actualDate.addDays(14)){
return 'false';
}
else{
return 'true';
}
as per your code and statement, i could find not find th necessity for the glideschedule and other lines of code.
btw what is meant by substr in var difHour = schedule.duration(actualDateTime,reqDateTime).getDurationValue().split(':')[0].substr(-2);
should not be it substring?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2018 10:26 AM
I tried your piece of code but it does not work. Any other way of making it work?
substr is substring, yes. In JavaScript we write it as substr. And substr(-2) means the two last digits of a number.
Also, the system property for this form is:
And the onLoad client script is:
function onLoad() {
var ajax = new GlideAjax('LFGGetProperty');
ajax.addParam('sysparm_name', 'getO365Dates');
ajax.getXML(HelloWorldParse);
function HelloWorldParse(response) {
var dates = response.responseXML.documentElement.getAttribute("answer");
var relDt = dates.split(',');
g_form.clearOptions('needed_by');
g_form.addOption('needed_by', '', '-- None --');
for (var i=0; i< relDt.length; i++) {
g_form.addOption('needed_by', relDt[i], relDt[i]);
}
g_form.setValue('needed_by', '');
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-31-2018 02:31 AM
The onload is not in sync with your Script include.
and also the script should be on change and not onload, because i am not sure if your field is set to a date by default or not.
as per the script include provided by me, it either returns true or false in string.
the client script is not checking for the similar values.
if i had to modify your client script:
function onLoad() {
var ajax = new GlideAjax('LFGGetProperty');
ajax.addParam('sysparm_name', 'getO365Dates');
ajax.addParam('sysparm_requestedDate', 'needed_by');
ajax.getXML(HelloWorldParse);
function HelloWorldParse(response) {
var dates = response.responseXML.documentElement.getAttribute("answer");
//var relDt = dates.split(',');
g_form.clearOptions('needed_by');
g_form.addOption('needed_by', '', '-- None --');
//for (var i=0; i< relDt.length; i++) {
// g_form.addOption('needed_by', relDt[i], relDt[i]);
//}
if(dates =='false'){
alert();
return false;
}
//g_form.setValue('needed_by', '');
}
}
and I think the substring would work only once you use the variables as string ,but not on the object of an array..
Also,please note that the right API should be slice that replaces substring,with that operations.
Please place logs and lets us know.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-31-2018 05:24 AM