Display values from reference field in a text field

othomas1
Kilo Guru

Hello all,

I have a catalog item im working on where im trying to populate some information based on the user in the requested for field, things are working except the location field and the manager field, can anyone spot what is wrong with my client script?

Client script: onChange

Variable name: requested_for

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

var empInfo = g_form.getReference('requested_for', userDetails);

function userDetails(empInfo){
g_form.setValue('employee_number', empInfo.user_name);
g_form.setValue('job_title', empInfo.title);
g_form.setValue('location', empInfo.location.name);
g_form.setValue('manager_name', empInfo.manager.name);
}
}

find_real_file.png

 

 

1 ACCEPTED SOLUTION

SanjivMeher
Kilo Patron
Kilo Patron

You can't do dot walking using getreference.

 

So your script should be

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

var empInfo = g_form.getReference('requested_for', userDetails);

function userDetails(empInfo){
g_form.setValue('employee_number', empInfo.user_name);
g_form.setValue('job_title', empInfo.title);
g_form.setValue('location', empInfo.location);
g_form.setValue('manager_name', empInfo.manager);
}
}

 

And you variables should be reference fields. So location should be a reference field to cmn_location and manager should be a reference field to sys_user.


Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

7 REPLIES 7

Mike Patel
Tera Sage

You cannot dot walk in client script you will have to use GlideAJAX to process this on server side and return results.

Client script

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}

	// Call the GA function
	var ga = new GlideAjax('u_userInfoAjax');
	ga.addParam('sysparm_name', "getInfo");
	ga.addParam('sysparm_user', g_form.getValue('requested_for'));
	ga.getXMLAnswer(function(answer){
		// Save the return to a global variable
		var info = JSON.parse(answer);
		g_form.setValue('location', info.location);
		g_form.setValue('manager_name', info.manager);
		g_form.setValue('employee_number', info.user_name);
		g_form.setValue('job_title', info.title);

	});

}

Script Includes

var u_userInfoAjax = Class.create();
u_userInfoAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getInfo: function(){
		var user = this.getParameter('sysparm_user');
		var gr = new GlideRecord("sys_user");
		gr.get(user);
		var location = gr.location.name;
		var manager = gr.manager.getDisplayValue();
		var title = gr.title;
		var user_name = gr.user_name;

		var response = {};
		response.location = location;
		response.manager = manager;
		response.title = title;
		response.user_name = user_name;

		return JSON.stringify(response);
	},

	type: 'u_userInfoAjax'
});

Would this still work in the service portal?

Yes, It will work everywhere.

SanjivMeher
Kilo Patron
Kilo Patron

You can't do dot walking using getreference.

 

So your script should be

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

var empInfo = g_form.getReference('requested_for', userDetails);

function userDetails(empInfo){
g_form.setValue('employee_number', empInfo.user_name);
g_form.setValue('job_title', empInfo.title);
g_form.setValue('location', empInfo.location);
g_form.setValue('manager_name', empInfo.manager);
}
}

 

And you variables should be reference fields. So location should be a reference field to cmn_location and manager should be a reference field to sys_user.


Please mark this response as correct or helpful if it assisted you with your question.