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

As I've pointed out earlier, Requested for is a reference field to User table.

find_real_file.png

Execution of the above script:

find_real_file.png

Hey Hitoshi, I appreciate the help so far. It is now printing out a blank value. Here is my script include as well as my client script again, changed to what you have suggested above. Maybe I am missing some syntax some where? Also I attempted putting several different string values in the 'user.getValue()' parenthesis which obviously didnt work either. Also, if we can get this to work, how would I go about printing it to a text field like you have done above? Thanks again a ton! I appreciate all the help so far. 

Script Include

var GetEmployeeID = Class.create();
GetEmployeeID.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getUserEmpID: function() {

        var _userSysid = this.getParameter('sysparm_user_sysid');
        var emp = "";
        var user = new GlideRecord('sys_user');
        user.addQuery('sys_id', _userSysid);
        user.query();
        if (user.get(_userSysid)) {
            emp = user.getValue();

        }
        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_sysid', newValue);
	ga.getXML(setUserValue);
	function setUserValue(response)
	{
		var ans = response.responseXML.documentElement.getAttribute("answer");
		console.log(ans);
		alert(ans);
	}
}

 

update as this

Script Include

var GetEmployeeID = Class.create();
GetEmployeeID.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getUserEmpID: function() {

        var _userSysid = this.getParameter('sysparm_user_sysid');
        var emp = "";
        var user = new GlideRecord('sys_user');
        user.addQuery('sys_id', _userSysid);
        user.query();
        if (user.get(_userSysid)) {
            emp = user.user_name;

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

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

OK. Complete information.

This is my Request page with fields "Requested for" and "User name"

find_real_file.png

I've designed the form as below with field "User name" having name "u_user_name".

find_real_file.png

 I then created Client Script as below.

find_real_file.png

 

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

Then I created Script Include as below:

find_real_file.png

 

var GetEmployeeID = Class.create();
GetEmployeeID.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getUserEmpID: function() {
        var _userSysid = this.getParameter('sysparm_user_id');
        var emp = "";
        var user = new GlideRecord('sys_user');
        if (user.get(_userSysid)) {
            emp = user.getValue('user_name');
        }
        return emp;
    },
    type: 'GetEmployeeID '
});

Execution result:

find_real_file.png

This is what I've changed.

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()
        //user.query();  // comment out
        //if (user.next()) {  // comment out
        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
        }
    });
	//ga.getXML(setUserValue);  // comment out all .getXML() code
	//function setUserValue(response)
	//{
	//	var ans = response.responseXML.documentElement.getAttribute("answer");
	//	console.log(ans);
	//	alert(ans);
	//}
}