How to get employee number of logged in user in UI Action script

now_dev
Mega Expert

Hi All,

I wanted to fetch employee number of logged in user using UI Action script. Can please anyone suggest !

1 ACCEPTED SOLUTION

Jim Coyne
Kilo Patron

Because your UI Action is client-side, you could use Session Client Data as explained in this blog post - Two ways to Reduce Client Side lookups



Or, as kalai mentioned in that blog post, a display Business Rule to add the Employee Number to the scratchpad - Client Script Best Practices - ServiceNow Wiki



Your Business Rule could look like:


Table:           whatever table the UI Action is on


Advanced:     checked


When:             Display


Script:


(function executeRule(current, previous /*null when async*/) {


  //add the Employee Number to the scratchpad


  var gr = new GlideRecord("sys_user");


  if (gr.get(gs.getUserID().toString())){


      g_scratchpad.u_employee_number = gr.getValue("employee_number");


  }



})(current, previous);



And then you should be able to access the value in your UI Action with g_scratchpad.u_employee_number.



NOTE: I modified the Business Rule to ensure compatibility with all versions of the platform.


View solution in original post

10 REPLIES 10

Doesn't work in Fuji.


Just be aware that getUser() fetches a cached version of the user's information.   Try this out in a dev instance by modifying your first name and employee number and then running the following code in a Background Script or James.Neale's excellent Xplore tool:



(function(){


  var userObj = gs.getUser();


  gs. ­print( ­userObj. ­getDisplayName());


  gs.print(userObj.getRecord().getValue("employee_number"));



  gs.print("");


  userObj = userObj. ­getUserByID( ­gs.getUserName());


  gs. ­print( ­userObj. ­getDisplayName());


  gs.print(userObj.getRecord().getValue("employee_number"));


})();



Here's my result in Helsinki:


*** Script: Jim Coyne


*** Script: 123


*** Script:


*** Script: Jim Updated Coyne


*** Script: Updated 1234



As you can see, getUser() returns my old information, as it was when I logged in.   You can retrieve the updated info by using getUserByID() on the resulting getUser() object (line 7).   You can "shorten" it by using gs.getUser().getUserByID(gs.getUserName()).getRecord().getValue("employee_number")  



By that point, you are probably better off performing a GlideRecord query directly.


Hi Abhinay,



My UI action is client side and can you please help in provide client side code.


try


var userid = g_user.userName;


Jim Coyne
Kilo Patron

Because your UI Action is client-side, you could use Session Client Data as explained in this blog post - Two ways to Reduce Client Side lookups



Or, as kalai mentioned in that blog post, a display Business Rule to add the Employee Number to the scratchpad - Client Script Best Practices - ServiceNow Wiki



Your Business Rule could look like:


Table:           whatever table the UI Action is on


Advanced:     checked


When:             Display


Script:


(function executeRule(current, previous /*null when async*/) {


  //add the Employee Number to the scratchpad


  var gr = new GlideRecord("sys_user");


  if (gr.get(gs.getUserID().toString())){


      g_scratchpad.u_employee_number = gr.getValue("employee_number");


  }



})(current, previous);



And then you should be able to access the value in your UI Action with g_scratchpad.u_employee_number.



NOTE: I modified the Business Rule to ensure compatibility with all versions of the platform.