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

Eli Hurst
Kilo Contributor

Hello all! 

So, I have attempted all the solutions here without success. I have attempted adding a console.log(); which outputs a blank value. I do get a red warning about ajax processing (see below). Keep in mind, I have attempted all 3 solutions from Hitoshi, Ankur, and Shloke. I appreciate all the help for real! Thanks a ton, but I am still confused why nothing prints. I do have a label variable with the name 'u_user_name' for the value to go to. Lastly I did recieve an error in the console in Chrome, which is also below. Any continuing assistance is appreciated! Currently my code stands as the following, which is the pink/yellow suggestion from Hitoshi. Thanks.


Console Error

[00:00:00.119] *** WARNING *** GlideAjax.getXMLWait - synchronous function - processor: AJAXGlideRecord
js_includes_doctype.jsx?v=01-04-2022_1744&lp=Fri_Feb_18_09_56_17_PST_2022&c=28_620:15768  *** WARNING *** GlideAjax.getXMLWait - synchronous function - processor: AJAXGlideRecord



Script Include

var GetEmployeeID = Class.create();
GetEmployeeID.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getUserEmpID: function() {
        var _userSysid = this.getParameter('sysparm_user_id'); // changed parameter name from "sysparam_user_name" to "sysparam_user_id" because sys_id is being passed instead of user name
        var emp = "";
        var user = new GlideRecord('sys_user');
        //user.addQuery('user_name', _userSysid); // since only getting 1 record, using .get()
        if (user.get(_userSysid)) { // insert this statement to get 1 record
            emp = user.getValue('user_name'); // get 'user_name' column

        }
        return emp;
    },
    type: 'GetEmployeeID '
});


Client Script

function onChange(control, oldValue, isLoading, newValue, isTemplate) {
    if (isLoading) {
        return;
    }
    var ajax = new GlideAjax('GetEmployeeID'); // changed variable name from "ga" to "ajax". This isn't necessary.
    ajax.addParam('sysparm_name', 'getUserEmpID');
    ajax.addParam('sysparm_user_id', newValue); // change parameter name from 'sysparm_user_name' to 'syspam_user_id'
    ajax.getXMLAnswer(function(answer) { // replaced .getXML() with .getXMLAnswer() because only getting 1 string value
        if (answer.length > 0) {
            g_form.setValue('u_user_name', answer); // set field "u_user_name" that I've added to the form with return value from Script Include
        }
    });

}

The error message doesn't seem to reflect the code. There's no getXMLWait() in the code. Check if there is any other script that's running on the form.

 GlideAjax.getXMLWait - synchronous function - processor: AJAXGlideRecord

From the Navigator, go to "System Definition" > "Client Scripts". Filter on column "Table" for name starting with "sc_req" to make sure there's only one client script.

I was able to find one script that could be affecting it. There were 2 total but only 1 was active, and here is the code. I assume a script that is set to "false" for its active status could not affect the code, thus I didnt share it here. Furthermore, I didnt see my script in that list, and I assume I should? Lastly I jumped into the table of scripts and looked for mine, and saw no 'Table' was listed while all other scripts did. Thanks again


Script: 

function onLoad() {
    g_form.setVariablesReadOnly(true);
    g_form.setReadOnly('equipment', false);
	g_form.setReadOnly('equipment_returns', false);
}

That doesn't seem to be it. Try changing Script Include to the following to see if the Script Include is actually being called. If nothing is set to field u_user_name, it means Script Include isn't being called.

var GetEmployeeID = Class.create();
GetEmployeeID.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getUserEmpID: function() {
        return "test user";
    },
    type: 'GetEmployeeID '
});

So I do see "test user" coming back from the script include in the console, but does place a value in the u_user_name field. So I think its connected properly. I attempted to put the original code back in place and then the console ouputs nothing.  I have no idea what I am missing. 

 

furthermore, I keept noticing this is being populated in the "default value" of the variable.

javascript: gs.getUser().getRecord().getValue('user_name') 

Will it matter whether the variable is a label, or single line text? I have tried both but I just wasnt sure.