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-19-2022 12:51 PM
Could you copy paste the Script Include and Client Script exactly as you have it?
Removing global is not a good idea.
To your original question on how to check field names, you can right-click on a field's label and select the Show - '...' context menu item:
Actually that already tells you the field name, but clicking on it reveals further details and enables you to copy the name:
When needing the same info for fields that are not on the form, one can bring up the xml view of records that contains the field name (and values):
Here's the XML view:
Also, if installing browser extensions is an option, many prefer using

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2022 06:14 PM
Hi,
The message suggests problem with scopes.
Is the scope of Client Script and Script Include the same?
I've re-created the Script Include as a scoped application.
Script Include. The content of the script is the same but I've renamed both the Client Script and Script Include to avoid conflict with those I've created under Global.
I've also re-created a Maintain Item as a scoped application in the same scope.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2022 06:20 PM
If the Script Include is created in Global scope, change as follows to use it from Client Script.
Script Include. Set "Accessible from" to "All application scopes".
Client Script in scoped
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//var ajax = new GlideAjax('GetUserDepartmentScoped');
var ajax = new GlideAjax('global.GetUserDepartment'); // prefix Script Include name with "global."
ajax.addParam('sysparm_name', 'getDepartment');
ajax.addParam('sysparm_userID', newValue);
ajax.getXMLAnswer(function(answer) {
if (answer.length > 0) {
g_form.setValue('department', answer);
}
});
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2022 11:37 AM
Konichiwa.
So I tried everything to make sure both the script include and the client script are in the global scope.
Script include:
var GetUserDepartment= Class.create();
GetUserDepartment.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
// Define the getDepartment function.
getDepartment: function() {
// Create a GlideRecord for the User table.
var userRecord = new GlideRecord("sys_user");
// Use the sysparm_userID passed from the client side
// to retrieve a record from the User table.
userRecord.get(this.getParameter('sysparm_userID'));
// Return the department for the requested record
return userRecord.department.name;
},
type: 'GetUserDepartment'
});
Client script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
var gaAjax = new GlideAjax('global.x_<API_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.getXMLAnswer(function(answer) {
if (answer.length > 0) {
g_form.setValue('department', answer);
}
});
}
Then I got this error message:
"Access to Script include blocked from scope."
I then tried to return to the landing page and set my scope to "Global" from the ???? menu (top right), but the "Blocked from scope" error message keeps coming back.
What should I do next?