The CreatorCon Call for Content is officially open! Get started here.

User details on client script

ceraulo
Mega Guru

Hello!

How do I get other current user details (example location, department, phone) in a client script?

Thank you.

1 ACCEPTED SOLUTION

Shweta KHAJAPUR
Tera Guru

Hi,

Write below code in Display BR

var gr = new GlideRecord('sys_user');
gr.get(gs.getUserID());
g_scratchpad.location = gr.location.name;
g_scratchpad.department = gr.department.name;
g_scratchpad.number = gr.mobile_phone;

Write Below code in onLoad client script

g_form.addInfoMessage(g_scratchpad.location); 
g_form.addInfoMessage(g_scratchpad.department); 
g_form.addInfoMessage(g_scratchpad.number); 

View solution in original post

9 REPLIES 9

Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

Hi,

If you talking about the logged in user, I would say that you use a Display Business rule to get the info and put that into the g_scratchpad. then you can reach that scratchpad in the client script.

 

Here is a good example: Example - retrieve server data using g_scratchpad | ServiceNow Docs

 

//Göran
Feel free to connect with me:
 

Omkar Mone
Mega Sage

Hi 

Create a 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();

gs.addInfoMessage(gr.location.city);
   }


   return JSON.stringify(obj);


   },
	
    type: 'GetUserDetailsAjax'
});

 

 

onChange Client Script :-

 

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


   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);


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


   }

   
}

 

Mark Correct if it helps.

 

Regards,

Omkar Mone.

www.dxsherpa.com

 

Pranay Tiwari
Kilo Guru

Hi,

If you want to achieve by client script then use g_user it will give you all information of user.

Pranay Tiwari
Kilo Guru

Hi,

 

If you want to fetch current user data in Client script then you can achieve it  by g_user object.And in server side you can fetch by Glide System function.