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

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).

find_real_file.png

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.

find_real_file.png

To set the single line text as read only, check the "Read only" check box.

find_real_file.png

@Eli Hurst Is it now displaying "test user" in in the u_user_name field?

Hi @Eli Hurst 

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

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

find_real_file.png

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.

@Eli Hurst I think I've found the problem. It's trying to set onChange on a dot walked variable.

Variable name is set to "Requestor Information.request_for". This won't work.

find_real_file.png