GlideAjax Returning Null From Script Include (Business Day Calculation)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago
Hi there,
I'm working on a client script + script include to automatically calculate a future business date based on a starting date selected on a form.
When a user selects a date in a field, an onChange client script sends that date to a Script Include. The Script Include uses a GlideSchedule to add a certain number of business days, then returns the resulting date back to the client script, which populates a "response due" field.
Client Script
function onChange(control, oldValue, newValue) {
console.log("[BusinessDayUtils Client] onChange triggered");
console.log("[BusinessDayUtils Client] Field Name:", control.name);
console.log("[BusinessDayUtils Client] Old Value:", oldValue);
console.log("[BusinessDayUtils Client] New Value:", newValue);
// If the field is cleared or empty, do nothing
if (!newValue){
console.log("[BusinessDayUtils Client] New value is empty. Exiting.");
return;
}
// Create a GlideAjaxa call to the Script Include 'BusinessDayUtils'
var ga = new GlideAjax('x_nyso3_foil.BusinessDayUtils');
console.log("[BusinessDayUtils Client] Created GlideAjax for Script Include.");
// Specify the function inside the Script Include we want to call (getBusinessDate)
ga.addParam('sysparm_name', 'getBusinessDate');
// Pass the start date (value from the field triggering onChange)
ga.addParam('sysparm_start_date', newValue);
// Pass the number of business days to add
ga.addParam('sysparm_days', '10');
console.log("[BusinessDayUtils Client] Parameters sent:");
console.log(" sysparm_name = getBusinessDate");
console.log(" sysparm_start_date =", newValue);
console.log(" sysparm_days = 10");
// Make async call
ga.getXMLAnswer(function(answer) {
console.log("[BusinessDayUtils Client] Received response from Script Include:", answer);
if (!answer) {
console.error("[BusinessDayUtils Client] ERROR: Server returned empty/undefined answer.");
return;
}
// Set form value
g_form.setValue('date_response_due', answer);
console.log("[BusinessDayUtils Client] date_response_due set to:", answer);
});
console.log("[BusinessDayUtils Client] GlideAjax request sent...");
}
BusinessDayUtils (Script Include)
var BusinessDayUtils = Class.create();
BusinessDayUtils.prototype = Object.extendsObject(global.AbstractAjaxProcessor,{
//isPublic: function() { return true; },
getBusinessDate: function () {
//TEST
gs.log("[TEST] Script Include Loaded");
// Force a syntax error:
notARealFunctionCall()
//TEST
// LOG
gs.log("[BusinessDayUtils] getBusinessDate() called");
// Read parameters
var startDate = this.getParameter('sysparm_start_date');
var daysToAdd = parseInt(this.getParameter('sysparm_days'), 10); // Covert the provided days to an integer
// LOG
gs.log("[BusinessDayUtils] Start Date (raw): " + startDate);
gs.log("[BusinessDayUtils] Days to Add (raw): " + daysToAdd);
//call internal function
var result = this._addBusinessDaysUsingSchedule(startDate, daysToAdd);
// LOG
gs.log("[BusinessDayUtils] Final Result Returned to Client: " + result);
return result;
},
_addBusinessDaysUsingSchedule: function (startDateStr, days) {
gs.log("[BusinessDayUtils] _addBusinessDaysUsingSchedule() called");
gs.log("[BusinessDayUtils] Input Start Date: " + startDateStr);
gs.log("[BusinessDayUtils] Input Days: " + days);
// schedule sys_id
var scheduleId = '090eecae0a0a0b260077e1dfa71da828';
gs.log("[BusinessDayUtils] Schedule sys_id: " + scheduleId);
// Instantiate GlideSchedule using the schedule record
var sched = new GlideSchedule(scheduleId);
var start = new GlideDateTime(startDateStr);
gs.log("[BusinessDayUtils] GlideDateTime Start (internal): " + start.getDisplayValue());
//Create a GlideDuration for X days
var dur = new GlideDuration();
dur.setDisplayValue(days + " 00:00:00");
gs.log("[BusinessDayUtils] GlideDuration created: " + dur.getDisplayValue());
var result = sched.add(start, dur);
if (result) {
gs.log("[BusinessDayUtils] Schedule Add Result (full datetime): " + result.getDisplayValue());
} else {
gs.log("[BusinessDayUtils] ERROR: sched.add() returned null or undefined");
}
// Return date only
var finalDate = result.getLocalDate().toString();
gs.log("[BusinessDayUtils] Final Local Date Returned: " + finalDate);
return finalDate;
},
type: 'BusinessDayUtils'
});
When the script include runs normally, the client receives null from getXMLAnswer().
However, if I put return "RETURN_TEST"; at the very top of the getBusinessDate(), the value does return correctly.
I added gs.log() statements as well but nothing appears in the logs, which is making it difficult to trace.
Has anyone dealt with a problem like this before? Any ideas about what could cause this script include to return null only?
Thanks in advance for any help offered!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
If you're not seeing even the first log, the script is encountering an error while loading. The notARealFunctionCall() isn't helping anything, so get rid of that, check the system log and browser console for any errors, then comment other lines in the function to find what it doesn't like. As a good practice, the SI should be coded to always return something whether that's an empty string 'false' or whatever so that you're sure it's running to the return rather than failing somewhere along the way.