- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks 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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Your userCompanyCheck does not return anything. compareCompany is an asynchronous callback function so when it is executed the outer function is long gone from the stack and it doesn't matter what you return from it.
You'd need to return a promise from your reusable function.
function onLoad() {
g_form.userCompanyCheck = function () {
return new Promise((resolve) => {
var user = "35826bf03710200044e0bfc8bcbe5d38a";
var company = "187d13f03710200044e0bfc8bcbe5df2";
var userCompany = new GlideAjax('Asd');
userCompany.addParam('sysparm_name', 'getUserCompany');
userCompany.addParam('sysparm_userid', user);
userCompany.getXMLAnswer((answer) => resolve(answer === company));
})
};
g_form.userCompanyCheck().then((r) => {
console.log(r)
})
}
Even if it were synchronous an inner function return value is not automatically returned from the outer function as they don't share execution context
asd = function () {
function hmm() {
return 1
}
hmm()
}
gs.info(asd())
//*** Script: undefined
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Your userCompanyCheck does not return anything. compareCompany is an asynchronous callback function so when it is executed the outer function is long gone from the stack and it doesn't matter what you return from it.
You'd need to return a promise from your reusable function.
function onLoad() {
g_form.userCompanyCheck = function () {
return new Promise((resolve) => {
var user = "35826bf03710200044e0bfc8bcbe5d38a";
var company = "187d13f03710200044e0bfc8bcbe5df2";
var userCompany = new GlideAjax('Asd');
userCompany.addParam('sysparm_name', 'getUserCompany');
userCompany.addParam('sysparm_userid', user);
userCompany.getXMLAnswer((answer) => resolve(answer === company));
})
};
g_form.userCompanyCheck().then((r) => {
console.log(r)
})
}
Even if it were synchronous an inner function return value is not automatically returned from the outer function as they don't share execution context
asd = function () {
function hmm() {
return 1
}
hmm()
}
gs.info(asd())
//*** Script: undefined
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hello, lauri.
Thank you - this worked for this case. While my implementation still isn't perfect, I am certain this is due to my Script Include calls. There are no more timing issues.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hello @Marc D ,
In this code :
if (userGr.get(userID)) {
var res = userGr.company;
return res;
}
company is a reference field so use .getDisplayValue() instead !
So use var res = userGr.getDisplayValue('company').toString();
If my response helped mark as helpful and accept the solution.

