- 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:29 PM
Yes. Field of type label won't be updated by a script.
I've tested it as below.
1. Created 3 variable. (1) type Label, (2) type Single Line Text, (3) type Single Line Text (read only).
2. Create a simple onLoad script.
function onLoad() {
g_form.setValue('label', 'test value');
g_form.setValue('single_line_text', 'test value2');
g_form.setValue('readonly_single_line_text', 'test value3');
}
3. Execution result. Value was not set on field of type Label.
To set the single line text as read only, check the "Read only" check box.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2022 02:34 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2022 10:10 PM
Hi
Error should not come from the code which I have shared. I have tested the same again in my PDI and works great. Can you check if there are any other script which might be affecting it.
I would suggest revert your script to my version and we can debug that further for you.
Regards,
Shloke
Regards,
Shloke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2022 06:22 AM
I set both the include and the client script to your code from above. These are the settings to my Client Script which I assume are correct. Secondly I have added a console.log(answer); right before the g.form.setValue at the end, and the console still brings up nothing more than a red error about GlideAjax.getXMLWait. I am truly confused why this wouldnt work, because it checks out in my head.

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