- 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-22-2022 11:11 PM
Hi
"I do have a label variable with the name 'u_user_name' " change the variable type to single line text and try the
Thanks

- 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 05:25 PM
Hey Hitoshi, I guess I was little confused before which was me just being dumb. I have since made a "requested_for_test" variable as a reference type pointed at the sys_user table with a condition on Active=true. However the initial "requested for" field that was dot walking was part of a variable set my SNOW team created. And it was interesting when I looked at the variable inside the variable set, I saw a variable attribute that I have pasted below. Furthermore I saw the default value was set to "javascript:gs.getUserID()" Not sure if that messes with anything. Secondly, I dont know if having both the pre-created Requested For field and my self created one makes any difference if they are both active but it shouldnt because my client script is pointed at the test field I made. I got the console.log to print out null but still nothing populates to my label field of u_user_name. My last question, is does the variable type matter for the output field? I was initially using a single line text "read only" field, but I have switched to a label field, so wondering about that. Honeslty out of ideas what else I could have incorrect. Also does the order number matter at all? Continuing assistance is greatly appreciated. Thanks Hitoshi, Schloke, and Ankur.
ref_auto_completer=AJAXTableCompleter,ref_ac_columns=user_name,ref_ac_columns_search=true,ref_ac_order_by=name

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2022 05:40 PM
>(1) Is "requested_for_test" in a variable set? If so, is the Client Script defined in the variable set or on the form? I've been creating Client Script on the form. It shouldn't matter but would like to know so I'll be able to test it on my PDI.
>(2)Secondly, I dont know if having both the pre-created Requested For field and my self created one makes any difference if they are both active but it shouldnt because my client script is pointed at the test field I made.
My bad. I thought there was only 1 Requested For field. Change the name of the one that's created to something else. "Question" can be the same but "name" should be unique. Was it already changed to "requested_for_test"? If so, onChange should be changed to use "requested_for_test".
>(3) My last question, is does the variable type matter for the output field? I was initially using a single line text "read only" field, but I have switched to a label field, so wondering about that.
As I've pointed out earlier, it does. Label fields can't be updated. Switch back to single line text.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2022 05:57 PM