- 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 05:49 PM
Try replacing "getElement()" with "getValue()" in Script Include.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2022 05:57 PM
Script Include. "Request For" is a reference field to sys_user table. This means it's a sys_id and not a "user_name".
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');
if (user.get(_userSysid)) {
emp = user.getValue('user_name');
}
return emp;
},
type: 'GetEmployeeID '
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2022 07:33 PM
I attempted using .getValue which didnt work. However it finally printed the console.log with a value of null. Any other fixes?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2022 07:58 PM
The provided script should work as it is. As I've pointed out, "Request For" will be sys_id and not "user_name" so the .addQuery() statement should be .addQuery('sys_id', _userSysid);
Since it's only going to return 1 record, can use .get(_userSysid); instead as I've shown above.