The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Getting employee ID to fill in a read only box

Eli Hurst
Kilo Contributor

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);
	}
}
1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

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.

https://community.servicenow.com/community?id=community_article&sys_id=1c10a1fedbbd4890feb1a851ca961...

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.

https://community.servicenow.com/community?id=community_question&sys_id=4d0e4baddb9cdbc01dcaf3231f96...

View solution in original post

44 REPLIES 44

Dot walked variable are retrieved synchronously, that's where the error message is coming from.

To test, try adding a new variable on the form that's a reference field to sys_user and assign the onChange to that new variable. The script should work.

I added 4 images of the variable field type, its specifications, and the updated client script settings per your instructions. I also noticed a pop up message (3rd image) in my script include section, and kinda thought that could be causing the issue?

find_real_file.png

 

 

 

find_real_file.png

 

 

find_real_file.png

 

 

 

 

 

find_real_file.png

Just to confirm. Is the variable for onChange now set to "request_for"? 

find_real_file.png

Is the variable "request_for" a reference to "sys_user" table?

find_real_file.png

The message can be ignored. ServiceNow used to allow developers to manipulate DOM from Client Script. They decided to disallow that by default because it ServiceNow may change the DOM structure in the new version. To keep compatibility, developers can override the default by unchecking "Isolate script". Unless doing DOM manipulation, it's better to keep it checked.

find_real_file.png

find_real_file.png

 

HAlso here is the Script Include options I have selected