- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2022 05:35 PM
Overview:
I want to grab the element string of 'user_name' from the table "sys_user" based on what user is populated in the "Request For" field of a request template. Then take the 'user_name' element and put it in a read only field farther down the request form. I was trying to get it to happen onChange. This is the code I have so far, and I am not sure what I am doing wrong here. The 2 pieces of code below are the script include as well as the client script. The console.log() and the alert() were just for my testing purposes. Any assisstance would be helpful. Thank you.
Script Include:
var GetEmployeeID = Class.create();
GetEmployeeID.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserEmpID: function() {
var _userSysid = this.getParameter('sysparm_user_name');
var emp = "";
var user = new GlideRecord('sys_user');
user.addQuery('user_name', _userSysid);
user.query();
if (user.next()) {
emp = user.getElement('user_name');
}
return emp;
},
type: 'GetEmployeeID'
});
Client Script
function onChange (control, oldValue, isLoading, newValue, isTemplate) {
if (isLoading) {
return;
}
var ga = new GlideAjax('GetEmployeeID');
ga.addParam('sysparm_name', 'getUserEmpID');
ga.addParam('sysparm_user_name', newValue);
ga.getXML(setUserValue);
function setUserValue(response)
{
var ans = response.responseXML.documentElement.getAttribute("answer");
console.log(ans);
alert(ans);
}
}
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2022 04:10 PM
FYI, all the codes offered by Ankur and shloke04 should also work.
The differences are as below:
1. I've used .getXMLAnswer instead of getXML in Client Script. Refer to the following article by Mark for the reason.
2. I've used .get() instead of .query() in Script Include. Refer to the following blog by Ben.
https://developer.servicenow.com/blog.do?p=/post/training-grget/
3. Used .getValue() in Script Include to get 'user_name'. Refer to reply by Chuck in the following thread.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2022 06:02 PM
My bad. I understand the situation now. The variables are defined in the variable set while the Client Script in defined on the form. It still should work.
Just to confirm about, changing the Script Include to the following still won't fill the user name after the field 'u_user_name" is changed to single line text?
var GetEmployeeID = Class.create();
GetEmployeeID.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserEmpID: function() {
return "test user";
},
type: 'GetEmployeeID '
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2022 08:30 PM
The script will return an error message if there's an error in the script.
var GetEmployeeID = Class.create();
GetEmployeeID.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserEmpID: function() {
try {
var _userSysid = this.getParameter('sysparm_user_id');
var emp = "";
var user = new GlideRecord('sys_user');
if (user.get(_userSysid)) {
emp = user.getValue('user_name');
}
return emp;
} catch (e) {
return e.message;
}
},
type: 'GetEmployeeID '
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2022 10:28 PM
So I got the "test user" to print out which gave me some hope. The test script include you gave me didnt seem to give me any output at all. Attached is my current scripting setup. The correct client script, and the error message script include you sent me most recently. I also sent my clien script settings as well as the last few lines of the Chrome console if any of it makes sense. It also doesn't seem to do anything when I change the employee at all. It feels like the "onChange" isn't really happening. Im hoping there is something thats an easy fix. Thanks again for the help.
Client Script
function onChange(control, oldValue, isLoading, newValue, isTemplate) {
if (isLoading) {
return;
}
var ajax = new GlideAjax('GetEmployeeID');
ajax.addParam('sysparm_name', 'getUserEmpID');
ajax.addParam('sysparm_user_id', newValue);
ajax.getXMLAnswer(function(answer) {
if (answer.length > 0) {
g_form.setValue('u_user_name', answer);
}
});
}
Script Include
var GetEmployeeID = Class.create();
GetEmployeeID.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserEmpID: function() {
try {
var _userSysid = this.getParameter('sysparm_user_id');
var emp = "";
var user = new GlideRecord('sys_user');
if (user.get(_userSysid)) {
emp = user.getValue('user_name');
}
return emp;
} catch (e) {
return e.message;
}
},
type: 'GetEmployeeID '
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2022 10:48 PM
Hi,
Please share catalog view of your catalog item.
Regards,
Mahesh

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2022 11:10 PM
Also want to make sure. In the User table (sys_user), the selected user does have "User ID" filled and is not empty. Field name "user_name" is the User ID column and not the "Name" column.
I'm copy & pasting back the script from this thread and it's working. It's not the script that's the problem. It's something else.