- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-10-2016 09:18 AM
Hi All,
I wanted to fetch employee number of logged in user using UI Action script. Can please anyone suggest !
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-10-2016 11:37 AM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-10-2016 09:58 AM
Doesn't work in Fuji.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-10-2016 11:12 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-10-2016 10:49 AM
Hi Abhinay,
My UI action is client side and can you please help in provide client side code.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-10-2016 10:59 AM
try
var userid = g_user.userName;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-10-2016 11:37 AM
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.