Get User's values from sys_user table on basis of user selected.

prabhmeet
Giga Expert

Hi,

Please find below screenshots of my catalog item. Requested for is a reference field to table sys_user. When I select a user's name in requested for, the user's values on the fields on right side should be automatically populated from sys_user table.

For eg - abel tuter is selected so first name, last name etc should be populated from sys_user table. The values which are not present should remain empty.

Please help me on how to do this with the code as I am new to ServiceNow and not very good at scripting..

find_real_file.png

find_real_file.png

1 ACCEPTED SOLUTION

Omkar Mone
Mega Sage

Hi 

You need to create a onChange client script and client callable script include for this purpose. See below :-

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
	
	var id = g_form.getValue('u_reference_1');//newValue


   var ga = new GlideAjax('GetUserDetailsAjax');


   ga.addParam('sysparm_name','getInfo');


   ga.addParam('sysparm_user_id',id);


   ga.getXML(CallBack);



   function CallBack(response)
	{


   var answer = response.responseXML.documentElement.getAttribute("answer");


   var user=JSON.parse(answer);


   //g_form.setValue('req_email', user.email);


   //g_form.setValue('req_phone', user.phone);


   //g_form.setValue('address1', user.street);
alert(user.location);

   //g_form.setValue('city_state_zip', user.location);


   }

   
}

 

Client Callable Script Include :-

 

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


   var obj={};
   obj.location='';
   obj.email='';
   obj.phone='';
   obj.street='';
   var id=this.getParameter('sysparm_user_id');
   var gr= new GlideRecord('sys_user');
   if(gr.get(id)){
	   
   if(gr.location!='')

   obj.location=gr.location.city.toString();
   obj.zip=gr.zip.toString();
   obj.street=gr.street.toString();
   obj.phone=gr.phone.toString();
   obj.email=gr.email.toString();

//here you will get all the required fields from sys_user that you want to show on your form.
   }


   return JSON.stringify(obj);


   },
	
    type: 'GetUserDetailsAjax'
});

 

Let me know.

 

Regards,

Omkar Mone.

www.dxsherpa.com

View solution in original post

30 REPLIES 30

Omkar Mone
Mega Sage

Hi 

You need to create a onChange client script and client callable script include for this purpose. See below :-

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
	
	var id = g_form.getValue('u_reference_1');//newValue


   var ga = new GlideAjax('GetUserDetailsAjax');


   ga.addParam('sysparm_name','getInfo');


   ga.addParam('sysparm_user_id',id);


   ga.getXML(CallBack);



   function CallBack(response)
	{


   var answer = response.responseXML.documentElement.getAttribute("answer");


   var user=JSON.parse(answer);


   //g_form.setValue('req_email', user.email);


   //g_form.setValue('req_phone', user.phone);


   //g_form.setValue('address1', user.street);
alert(user.location);

   //g_form.setValue('city_state_zip', user.location);


   }

   
}

 

Client Callable Script Include :-

 

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


   var obj={};
   obj.location='';
   obj.email='';
   obj.phone='';
   obj.street='';
   var id=this.getParameter('sysparm_user_id');
   var gr= new GlideRecord('sys_user');
   if(gr.get(id)){
	   
   if(gr.location!='')

   obj.location=gr.location.city.toString();
   obj.zip=gr.zip.toString();
   obj.street=gr.street.toString();
   obj.phone=gr.phone.toString();
   obj.email=gr.email.toString();

//here you will get all the required fields from sys_user that you want to show on your form.
   }


   return JSON.stringify(obj);


   },
	
    type: 'GetUserDetailsAjax'
});

 

Let me know.

 

Regards,

Omkar Mone.

www.dxsherpa.com

Hi Omkar,

Thanks a lot for your answer. It is working correctly.

Only one issue that for some users the first name is not displaying. Like for eg abraham lincoln or Prabhmeet or System Administrator. For many more I checked they are displaying

Why do you think this is happening?

 

 

Hi 

Are you sure it is present in the sys_user table for the same users?

Hi Omkar, 

Yes, these users are present in sys_user table and moreover their last name is displaying, but their first name is not displaying in the field.

Till now, I only found 3 users with this case - Abraham lincoln , Prabhmeet Kaur ,System Administrator.