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-17-2022 08:34 PM
Hi,
This script looks good.
Can you please share your client script part?
Want to see how you are calling it.
Make sure to use script include API Name if you are calling it in scoped application.
Your code is client script should be like below:
var gaAjax = new GlideAjax('scope_name.GetUserDepartment'); // use script include api name here
gaAjax.addParam('sysparm_name','getDepartment');
gaAjax.addParam('sysparm_userID', newValue); // pass the user sys_id here
gaAjax.getXML(callback); // or use getXMLAnswer or getXMLWait
Thanks,
Anil Lande
Thanks
Anil Lande
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2022 08:53 PM
Hi,
Try below code in your client script
var ga = new GlideAjax('scope_name.GetUserDepartment'); // use script include name here
ga.addParam('sysparm_name','getDepartment');
ga.addParam('sysparm_userID', newValue); // if you're using onChange on user field/variable then pass newValue here if you're using any other client script then pass sys_id of the user
ga.getXMLAnswer(callback);
function callback(response) {
var answer = response; // answer holds the response of script include
g_form.setValue("FIELD_NAME",answer)
}
Please mark my response as correct answer or helpful if applicable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2022 01:50 PM
Hello.
So, below is the client script that I wrote, based on the responses I got above:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
var gaAjax = new GlideAjax('x_123456_<app_name>_0.GetUserDepartment');
gaAjax.addParam('sysparm_name', 'getDeaprtment');
// IF using onChange on user field/variable
// THEN pass newValue here
// IF you're using any other client script
// 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;
g_form.setValue('department', answer_Dept);
}
}
And here's the response I got:
What should I do?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2022 02:06 PM
Refer article for a check.