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

The problem is happening in the Script Include.

Just to make sure, is "request_for_test" a reference field to "sys_user" table?

find_real_file.png

Also want to make sure. In the User table (sys_user), the selected user does have "User ID" filled and is not empty. Field name "user_name" is the User ID column and not the "Name" column.

I'm copy & pasting back the script from this thread and it's working. It's not the script that's the problem. It's something else.

To determine if onChange is being execute, try putting alert(). Following code will popup a message when the onChange is executed and when a value is returned from Script Include

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        return;
    }
	alert("onChange");
    var ajax = new GlideAjax('GetEmployeeID');
    ajax.addParam('sysparm_name', 'getUserEmpID');
    ajax.addParam('sysparm_user_id', newValue);
    ajax.getXMLAnswer(function(answer) {
		alert(answer);
        if (answer.length > 0) {
            g_form.setValue('u_user_name', answer);
        }
    });
}

I don't think it matters too much but try checking "Isolate script".

find_real_file.png

Ok, so I did attempt all of this. And yes the variable I created was a reference variable with the name 'requested_for_test' calling the 'sys_user' table. It was set to output to a single line text variable with the name 'u_usser_name' and the script was set to onChange. I attempted the alert("onChange"); as you mentioned, and I did see it pop up, but only when I would remove the name from my 'requsted_for_test' field, and in the u_user_name nothing would show up, it remained blank the whole time. I also attempted to Isolate Script option, which had no effect. The only last thing I could find was a similar field calling a contact number had a very similar script. I dug into it more, and saw it had an Access Control script attached to it by one of our developers. Would that make a difference, or could our senior developers have some level of access/control that I dont?

Yes. ACL may cause the field not to be updated. If the value from Script Include is showing in the alert but not in the field, ACL may be the cause. Recommend contacting senior developers on getting information on how a field can be updated using client script because the senior developers may have setup an ACL to block fields from being updated by client scripts.