GlideAjax returning Null

Hayden Reid
Tera Guru

Good evening,

 

I have been trying to figure out why my Script Include returns null.. I am trying to return the number of business days between two dates. I have been struggling with logging any kind of message to see if the parameters are being successfully passed to the Script Include which leaves me without much to go from.

 

Script Include :

 

ScriptInclude.png

 

getBusinessDaysBetween: function(startDate, endDate) {
gs.addInfoMessage('script include triggered');
var businessDays = 0;
// Convert the input dates to UTC and remove the time component
var start = new Date(Date.UTC(startDate.substr(0,4), startDate.substr(5,2) - 1, startDate.substr(8,2)));
var end = new Date(Date.UTC(endDate.substr(0,4), endDate.substr(5,2) - 1, endDate.substr(8,2)));
// Loop through each day between startDate and endDate
var currentDate = new Date(start);
while (currentDate <= end) {
var dayOfWeek = currentDate.getUTCDay();
// If the current day is not a weekend (Saturday or Sunday), increment the business day counter
if (dayOfWeek != 0 && dayOfWeek != 6) {
businessDays++;
}
// Move to the next day
currentDate.setDate(currentDate.getDate() + 1);
}
return businessDays;
},

 

 

Client Script:

ClientScript.png

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

//Call script include
var business_days = new GlideAjax('sn_hr_core.kpmg_hrUtils');
business_days.addParam('sysparm_name','getBusinessDaysBetween');
business_days.addParam('startDate', g_form.getValue('u_start_date'));
business_days.addParam('endDate', g_form.getValue('u_end_date'));
business_days.getXML(getResponse);


function getResponse(response) {
var diff = response.responseXML.documentElement.getAttribute('answer');
// Check to see if the difference between start and end is <= 15, set var = true for comparison in UI policy
alert(diff);
if (diff <= 15) {
g_form.setValue('total_unpaid_time', "true");
// Clear value if user changes their end date and is > 15
} else if (int_diff > 15) {
g_form.clearValue('total_unpaid_time');
}
}
}

 

3 REPLIES 3

BharathChintala
Mega Sage

@Hayden Reid  

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

var business_days = new GlideAjax('sn_hr_core.kpmg_hrUtils');
business_days.addParam('sysparm_name','getBusinessDaysBetween');
business_days.addParam('sysparm_startDate', g_form.getValue('u_start_date'));//changed here
business_days.addParam('sysparm_endDate', g_form.getValue('u_end_date'));//changed here
business_days.getXML(getResponse);

 

function getResponse(response) {
var diff = response.responseXML.documentElement.getAttribute('answer');
// Check to see if the difference between start and end is <= 15, set var = true for comparison in UI policy
alert(diff);
if (diff <= 15) {
g_form.setValue('total_unpaid_time', "true");
// Clear value if user changes their end date and is > 15
} else if (int_diff > 15) {
g_form.clearValue('total_unpaid_time');
}
}
}

 

Script Include:

getBusinessDaysBetween: function() {// changed here

var startDate =this.getParameter('sysparm_startDate');//added this

var endDate =this.getParameter('sysparm_endDate');// Added this 


gs.addInfoMessage('script include triggered');
var businessDays = 0;
// Convert the input dates to UTC and remove the time component
var start = new Date(Date.UTC(startDate.substr(0,4), startDate.substr(5,2) - 1, startDate.substr(8,2)));
var end = new Date(Date.UTC(endDate.substr(0,4), endDate.substr(5,2) - 1, endDate.substr(8,2)));
// Loop through each day between startDate and endDate
var currentDate = new Date(start);
while (currentDate <= end) {
var dayOfWeek = currentDate.getUTCDay();
// If the current day is not a weekend (Saturday or Sunday), increment the business day counter
if (dayOfWeek != 0 && dayOfWeek != 6) {
businessDays++;
}
// Move to the next day
currentDate.setDate(currentDate.getDate() + 1);
}
return businessDays;
},

 

I didn't check rest script hope these were issues. Always use sys_parm_'variable' to pass parameters from client script

to fetch to in script include use like below

var startDate =this.getParameter('sysparm_startDate');

 

If my inputs have helped with your question, please mark my answer as accepted solution, and give a thumb up.
Bharath Chintala

Hi @BharathChintala,

 

Thank you for the detailed response. I did make the suggested changes, however that did not fix the issue. I did actually find the resolution, which had something to do with the absence of the class not having the "Object.extendsObject(global.AbstractAjaxProcessor" being called at the class level. I moved my script include over to another scoped Script Include and I was able to call it successfully there.

 

 

Regardless, thank you for your feedback!

Hayden Reid
Tera Guru

@BharathChintala Hi there! I've adjusted the script to do what you suggested but it seems that I am still returning a null object after retrieving the XML... Perhaps there is another syntax issue with the var response? I've underlined that code in the Client script. Any other thoughts or suggestions on how I can confirm the parameters are being successfully passed?

 

Script Include:

 

getBusinessDaysBetween: function() {
gs.addInfoMessage('script include triggered');
var startDate = this.getParameter('sysparm_startDate');
var endDate = this.getParameter('sysparm_endDate');
var businessDays = 0;
// Convert the input dates to UTC and remove the time component
var start = new Date(Date.UTC(startDate.substr(0,4), startDate.substr(5,2) - 1, startDate.substr(8,2)));
var end = new Date(Date.UTC(endDate.substr(0,4), endDate.substr(5,2) - 1, endDate.substr(8,2)));
// Loop through each day between startDate and endDate
var currentDate = new Date(start);
while (currentDate <= end) {
var dayOfWeek = currentDate.getUTCDay();
// If the current day is not a weekend (Saturday or Sunday), increment the business day counter
if (dayOfWeek != 0 && dayOfWeek != 6) {
businessDays++;
}
// Move to the next day
currentDate.setDate(currentDate.getDate() + 1);
}
return businessDays;

 

Catalog Client Script:

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

//Call script include
var business_days = new GlideAjax('sn_hr_core.kpmg_hrUtils');
business_days.addParam('sysparm_name','getBusinessDaysBetween');
business_days.addParam('sysparm_startDate', g_form.getValue('u_start_date'));
business_days.addParam('sysparm_endDate', g_form.getValue('u_end_date'));
business_days.getXML(getResponse);


function getResponse(response) {
var diff = response.responseXML.documentElement.getAttribute('answer');
// Check to see if the difference between start and end is <= 15, set var = true for comparison in UI policy
alert(diff);
if (diff <= 15) {
g_form.setValue('total_unpaid_time', "true");
// Clear value if user changes their end date and is > 15
} else if (int_diff > 15) {
g_form.clearValue('total_unpaid_time');
}
}
}