Requesting help with script include & client script for retrieving user's department
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2022 02:29 PM
Greetings, everyone.
I'm attempting to retrieve a user's department from the sys_user
table.I carefully went through the GlideAjax exercise, but I got stuck at the client script, which mentions a method called getEmail
, since I have no idea where this method was defined or how I can define a method to retireve a user's department instead.
Below is the Script include that I managed to complete:
var GetUserDepartment = Class.create();
// Extend the global.AbstractAjaxProcessor class
GetEmailAddress.prototype = Object.extendsObject(global.AbstractAjaxProcessor,{
getDepartment: function() { // Define the getDepartment function.
var userRecord = new GlideRecord("sys_user"); // Create a GlideRecord for the User table.
userRecord.get(this.getParameter('sysparm_userID')); // Use the sysparm_userID passed from the client side to retrieve a record from the User table.
return userRecord.department + ''; // Return the department for the requested record
},
type: 'GetUserDepartment'
});
(BTW, does anyone know how I can check the computer-friendly names of the columns in the sys_user
table?)
Would anyone be kind enough to give me hints on how I can complete the client script?
Any help would be greatly appreciated!
- Labels:
-
Scripting and Coding
-
Studio

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2022 07:13 PM
Hi,
You are using getXML(), in this case we need to use getXMLAnswer().
Please check below article to learn more:
You can update your client script like below:
// THEN pass sys_id of the user
gaAjax.addParam('sysparm_userID', newValue);
gaAjax.getXMLAnswer(callback); // one small change here
function callback(response) {
// answer_Dept holds the response of script include
var answer_Dept = response;
g_form.setValue('department', answer_Dept);
}
Or use below logic with response.responseXML.documentElement.getAttribute("answer"):
// THEN pass sys_id of the user
gaAjax.addParam('sysparm_userID', newValue);
gaAjax.getXML(callback);
function callback(response) {
// answer_Dept holds the response of script include
var answer_Dept = response.responseXML.documentElement.getAttribute("answer"); / or change this line
g_form.setValue('department', answer_Dept);
}
Thanks,
Anil Lande
Thanks
Anil Lande

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2022 07:06 PM
Hi,
Did you get chance to look at the solution proposed by me?
Thanks,
Anil Lande
Thanks
Anil Lande
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2022 08:52 AM
Checking right now.
Apologies for the lack of response. I'm the only developer, and I have to go back and forth between multiple user stories. =P

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2022 08:08 PM
Hi Cédric,
First, is the field Department a reference field to cmn_department table or a Single Line Text? If the original example was using email, it's probably a Single Line Text. However, department in user table is a reference field so returning value of department will only show sys_id of user's department.
Case 1: Department is a reference field to table cmn_department.
Client script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ajax = new GlideAjax('GetUserDepartment');
ajax.addParam('sysparm_name', 'getDepartment');
ajax.addParam('sysparm_userID', newValue);
ajax.getXMLAnswer(function(answer) {
if (answer.length > 0) {
g_form.setValue('department', answer);
}
});
}
Script include. I've changed "GetEmailAddress.prototype " to "GetUserDepartment.prototype"
var GetUserDepartment= Class.create();
GetUserDepartment.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getDepartment: function() { // Define the getDepartment function.
var userRecord = new GlideRecord("sys_user"); // Create a GlideRecord for the User table.
userRecord.get(this.getParameter('sysparm_userID')); // Use the sysparm_userID passed from the client side to retrieve a record from the User table.
return userRecord.department + ''; // Return the department for the requested record
},
type: 'GetUserDepartment'
});
Execution result
Case 2: Department is a Single Line Text
Client script is the same as above.
Script include
var GetUserDepartment= Class.create();
GetUserDepartment.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getDepartment: function() { // Define the getDepartment function.
var userRecord = new GlideRecord("sys_user"); // Create a GlideRecord for the User table.
userRecord.get(this.getParameter('sysparm_userID')); // Use the sysparm_userID passed from the client side to retrieve a record from the User table.
return userRecord.department.name; // Return the department for the requested record
},
type: 'GetUserDepartment'
});
Execution result
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2022 11:34 AM
Konichiwa!
I tried your script include, and I got this error message:
But when I replaced "Object.extendsObject(AbstractAjaxProcessor, {
" with "Object.extendsObject(global.AbstractAjaxProcessor, {
", the script include simply stopped working.
What should I do? (╥︣﹏᷅╥)