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

manredd
Kilo Expert

Try


gs.getUser().getRecord().getValue('employee_number')