- 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-21-2022 08:05 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2022 10:07 PM
Hey Hitoshi, I appreciate the help so far. It is now printing out a blank value. Here is my script include as well as my client script again, changed to what you have suggested above. Maybe I am missing some syntax some where? Also I attempted putting several different string values in the 'user.getValue()' parenthesis which obviously didnt work either. Also, if we can get this to work, how would I go about printing it to a text field like you have done above? Thanks again a ton! I appreciate all the help so far.
Script Include
var GetEmployeeID = Class.create();
GetEmployeeID.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserEmpID: function() {
var _userSysid = this.getParameter('sysparm_user_sysid');
var emp = "";
var user = new GlideRecord('sys_user');
user.addQuery('sys_id', _userSysid);
user.query();
if (user.get(_userSysid)) {
emp = user.getValue();
}
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_sysid', newValue);
ga.getXML(setUserValue);
function setUserValue(response)
{
var ans = response.responseXML.documentElement.getAttribute("answer");
console.log(ans);
alert(ans);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2022 10:57 PM
update as this
Script Include
var GetEmployeeID = Class.create();
GetEmployeeID.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserEmpID: function() {
var _userSysid = this.getParameter('sysparm_user_sysid');
var emp = "";
var user = new GlideRecord('sys_user');
user.addQuery('sys_id', _userSysid);
user.query();
if (user.get(_userSysid)) {
emp = user.user_name;
}
return emp;
},
type: 'GetEmployeeID'
});
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2022 12:32 AM
OK. Complete information.
This is my Request page with fields "Requested for" and "User name"
I've designed the form as below with field "User name" having name "u_user_name".
I then created Client Script as below.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (newValue === '') {
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);
}
});
}
Then I created Script Include as below:
var GetEmployeeID = Class.create();
GetEmployeeID.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserEmpID: function() {
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;
},
type: 'GetEmployeeID '
});
Execution result:

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2022 12:41 AM
This is what I've changed.
Script Include:
var GetEmployeeID = Class.create();
GetEmployeeID.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserEmpID: function() {
var _userSysid = this.getParameter('sysparm_user_id'); // changed parameter name from "sysparam_user_name" to "sysparam_user_id" because sys_id is being passed instead of user name
var emp = "";
var user = new GlideRecord('sys_user');
//user.addQuery('user_name', _userSysid); // since only getting 1 record, using .get()
//user.query(); // comment out
//if (user.next()) { // comment out
if (user.get(_userSysid)) { // insert this statement to get 1 record
emp = user.getValue('user_name'); // get 'user_name' column
}
return emp;
},
type: 'GetEmployeeID '
});
Client Script:
function onChange (control, oldValue, isLoading, newValue, isTemplate) {
if (isLoading) {
return;
}
var ajax = new GlideAjax('GetEmployeeID'); // changed variable name from "ga" to "ajax". This isn't necessary.
ajax.addParam('sysparm_name', 'getUserEmpID');
ajax.addParam('sysparm_user_id', newValue); // change parameter name from 'sysparm_user_name' to 'syspam_user_id'
ajax.getXMLAnswer(function(answer) { / replaced .getXML() with .getXMLAnswer() because only getting 1 string value
if (answer.length > 0) {
g_form.setValue('u_user_name', answer); // set field "u_user_name" that I've added to the form with return value from Script Include
}
});
//ga.getXML(setUserValue); // comment out all .getXML() code
//function setUserValue(response)
//{
// var ans = response.responseXML.documentElement.getAttribute("answer");
// console.log(ans);
// alert(ans);
//}
}