auto populate email id when name is selected from the reference field(sys_user)

Sneha39
Mega Guru

auto populate email id when name is selected from the reference field(sys_user)

Form fields

Name(reference from sys_user)

email( to be populate)

tried client script on variable set but its not working,

 

Kindly help with this simple script.

 

Thanks

1 ACCEPTED SOLUTION

dvp
Mega Sage
Mega Sage

Here is an onChange catalog client script

Please update the variable names in the below script

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


   var usr = g_form.getReference('USER_VARIABLE_NAME', callBack);
	
}

function callBack(usr)
{

	g_form.setValue('EMAIL_VARIABLE_NAME', usr.email);


}

View solution in original post

10 REPLIES 10

May I know why you gave two times  caller.email in setValue?

Sneha39
Mega Guru

One more thing to add i forgot to select variable name while applying this script on variable set so it wasn't working.( This was a reason as well) Just in case if above script is not working for anyone.

Thanks dvp

If the issue is resolved, kindly mark the comment as correct and helpful so that the question is removed from the unanswered list.

Vismit Ambre
Giga Guru

Hi Sneha,

You can try using the below function

 

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

 var _user = g_form.getValue('name');
 var userGR = new GlideRecord('sys_user');
 userGR.addQuery('sys_id', _user);
 userGR.query();
 if(userGR.next())
  {
   g_form.setValue('email', userGR.email);
  }

  
}

 

'name' & 'email' is to be replaced by your username & email variables respectively.

 

this is not working in the portal, any idea why??