- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
Included in the Developer Toolbox Update Set available on Share (link to Share in the post).
The GlideUser API gives us some nice methods to retrieve information on the currently logged-in user. Things like the display name, first/last name, username, email address, etc... Pretty limited though. There's an undocumented "getRecord()" method that allows you get some more information, but because it is undocumented, it is difficult to use.
We are all used to using GlideRecords, so I figured why not create a class that will return an actual GlideRecord we can use and take advantage of. Here's the details on the Script Include:
Name: FpcUser
Client callable: NOT checked
Accessible from: All application scopes
Active: Checked
Script:
var FpcUser = Class.create();
FpcUser.prototype = {
initialize: function() {
},
getUserRecord: function(user){
//validate if a value was passed in as a parameter
var user = (typeof user !== "undefined") ? user : gs.getUserID(); //default to logged-in user if a value is not passed in
if (user.trim() == "") {
user = gs.getUserID(); //default to logged-in user if the parameter is an empty/blank string
}
//setup an empty GlideRecord to return if the user is not found
var result = new GlideRecord("sys_user");
//try to find the User record based on sys_id first, then user_name
var gr = new GlideRecord("sys_user");
//trying with the sys_id
if (gr.get("sys_id", user)){ //updated to include the name of the sys_id field instead of relying on default behaviour
result = gr;
} else if (gr.get("user_name", user)) { //trying with the user_name
result = gr;
}
return result;
},
type: 'FpcUser'
};
You pass in a sys_id or user_name as a parameter in order to specify a specific user record that you want. Or if you want the currently logged-in user, just skip the parameter and the method will default to the logged-in user's record.
Here's an example script which uses both the gs.getUser() and the new class to retrieve some information:
var user = "";
var log = [];
//get some user info using gs.getUser()
gsUser = gs.getUser().getRecord();
log.push(gsUser);
log.push(gs.getUser().getDisplayName());
log.push(gs.getUser().getName());
log.push(gs.getUser().getEmail());
log.push(gsUser.getValue("title"));
log.push(gsUser.getDisplayValue("manager"));
log.push(gsUser.manager.user_name);
log.push(gsUser.manager.title);
//now with the new FpcUser class
var fpcUser = new global.FpcUser().getUserRecord(user);
log.push(fpcUser);
log.push(fpcUser.getDisplayValue());
log.push(fpcUser.getValue("user_name"));
log.push(fpcUser.getValue("email"));
log.push(fpcUser.getValue("title"));
log.push(fpcUser.manager.getDisplayValue());
log.push(fpcUser.manager.user_name);
log.push(fpcUser.manager.title);
log.push(fpcUser.manager.email.toString());
log;
Change the first line to include a sys_id or a user_name to return a specific record.
If you run the script in the excellent Xplore: Developer Toolkit, you'll see some interesting results:
First thing you will notice is the getRecord() method does not return an actual GlideRecord, but the class does. When I try to get some info on the manager, you'll see I can't dot-walk to get more info with getRecord(), but the new class allows full dot-walking because of the GlideRecord that is returned. Maybe there is a way with getRecord(), but again, it's undocumented, so good luck using it.
And there's an added benefit to using the new class - it will return the most current information in the records, and not cached information from the last login.
Here are some example use cases:
Get the logged-in user's Manager's email address
var managerEmail = new global.FpcUser().getUserRecord().manager.email.toString();
Get the logged-in user's phone number:
var phone = new global.FpcUser().getUserRecord().getValue("phone");
Set default value to logged-in users's email address:
javascript:new global.FpcUser().getUserRecord().getValue("email");
Set default value to logged-in users's email address, but from a scoped app (thanks John):
javascript:new global.FpcUser().getUserRecord().getValue("email")
I've attached an XML export of the Script Include record if you want to just import it into your instance. Remember to try it out in your own personal dev instance first.
Update August 30, 2021
I've updated the code to return a blank/empty GlideRecord object if the User record is not found. Also updated the example code with the "global" scope prefix.
- 976 Views
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.