Called Client Script Returning Undefined
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago - last edited 2 hours ago
Hello, everyone. I'm having an issue with an implementation of a solution for my client.
I have been tasked for a form to compare the company of a user on the form with another company listed on the form itself, and to make certain fields read-only if they are identical. This is in and of itself fairly simple - a GlideAjax call.
However, I have to use this same functionality multiple times over multiple Client Scripts, and to cut down on the same GlideAjax call being written repeatedly, I placed it within a function within a separate Client Script and had my main Client Scripts call this function.
This is how it looks:
Script Include:
getUserCompany: function() {
var userID = this.getParameter('sysparm_userid');
if (userID != "") {
var userGr = new GlideRecord("sys_user");
if (userGr.get(userID)) {
var res = userGr.company;
return res;
}
}
return "";
}
Callable Client Script:
userCompanyCheck = function() {
var user = g_form.getValue('form_user');
var company = g_form.getValue('form_company');
var userCompany = new GlideAjax('<Script Include Name>');
userCompany.addParam('sysparm_name', 'getUserCompany');
userCompany.addParam('sysparm_userid', user);
userCompany.getXML(compareCompany);
function compareCompany(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer != company) {
return 'false';
} else {
return 'true';
}
}
};Main Client Script:
var userHasSameCompany = userCompanyCheck();
if (userHasSameCompany == 'true') {
g_form.setValue('internal_comparison_flag', 'No');
} else {
g_form.setValue('internal_comparison_flag', 'Yes');
}
But there's an issue with this: in the main Client Script, userHasSameCompany ALWAYS returns "undefined" from userCompanyCheck(), regardless of whether or not it returned "true" or "false", and I have checked - it has reached both.
What is the problem with my implementation? Did I do something wrong?
