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

Kalaiarasan Pus
Giga Sage

There are predefined functions to get userID or sys id of the logged in user


getUserID()


getUserName()



More info:


GlideSystem - ServiceNow Wiki


Abhinay Erra
Giga Sage

Use this line in your UI action script to get the logged in user's employee number


gs.getUser().getRecord().getValue("employee_number");


Works in which release?


Works in G, H. Did not test on Fuji