Service portal on change client script to update form variables based on selected reference variable

kylem1
Tera Guru

I could use some help with an onChange client script for a catalog item on the service portal.

A variable called updateEmployeeProfile references the sys_user table. When the field is selected, I would like to update form variables like updateEmployeeFirstName and updateEmployeeLastName from the sys_user table first_name and last_name.  

The variables do not update when updateEmployeeProfile variable is changed both on service portal and IT side.Thank you in advance!

---

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

//Type appropriate comment here, and begin script below

var user = new GlideRecord("sys_user");

user.addQuery("user_name", g_form.getValue('updateEmployeeProfile'));

user.query();

if(user.next())
{
var userid = user.sys_id;
g_form.setValue('updateEmployeeFirstName', userid.first_name);
g_form.setValue('updateEmployeeLastName', userid.last_name);
}

}

1 ACCEPTED SOLUTION

Brian Lancaster
Tera Sage

You cannot use GlideRecord in the Service Portal.  Change you script to something like below.  You will just need to substitute your variables names and add code for things like first name last name.  My script below is just pulling phone number and email.

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	
	//Type appropriate comment here, and begin script below
	g_form.getReference('primary_developer_name', function(ref){
		g_form.setValue('primary_developer_phone', ref.phone);
		g_form.setValue('primary_developer_email', ref.email);
	});
	
}

View solution in original post

2 REPLIES 2

Brian Lancaster
Tera Sage

You cannot use GlideRecord in the Service Portal.  Change you script to something like below.  You will just need to substitute your variables names and add code for things like first name last name.  My script below is just pulling phone number and email.

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	
	//Type appropriate comment here, and begin script below
	g_form.getReference('primary_developer_name', function(ref){
		g_form.setValue('primary_developer_phone', ref.phone);
		g_form.setValue('primary_developer_email', ref.email);
	});
	
}

kylem1
Tera Guru

You rock! Thank's for the quick response. This solved my issue.