logo

NJP

GlideUser/g_user in ServicePortal

Import · Feb 01, 2018 · article

The GlideUser API provides access to information about the current user and current user roles. You can access these APIs via a global object called g_user which is typically used in client scripts and UI policies.

In ServicePortal we don't have access to g_user global object in widget client script.

For example: If you have ever try to use g_user in widget client script like below.

image

UI breaks saying "g_user is not defined".

image

So if we want to get logged in users SysId or his email etc in widget client script, do we have to do a GlideRecord query in widget Server script and then pass it to Client script?

Fortunately, ServicePortal provides us with a service called glideUserSession, you just inject this into your client script and you are good to go. Now let's see how to use this in our portal widgets.

Let's inject the service into our client script.

image

Now that we have injected the gliderUserSession into our client script, let's see how to use it.

Widget Client Script demonstrating gliderUserSession APIs

function(glideUserSession) {

/* widget controller */

var c = this;

//glideUserSession provided OOTB used to like g_user

glideUserSession.loadCurrentUser().then(function(currentUser) {

//To get userID. Equivalent to g_user.userID

console.log(currentUser.userID);

//To get userName. Equivalent to g_user.userName

console.log(currentUser.userName);

//To get firstName. Equivalent to g_user.firstName

console.log(currentUser.firstName);

//To get lastName. Equivalent to g_user.lastName

console.log(currentUser.lastName);

//To get fullName. Equivalent to g_user.getFullName()

console.log(currentUser.getFullName());

//To check current user has specified role or admin role. Equivalent to g_user.hasRole('admin');

console.log(currentUser.hasRole('admin'));

//To check current user has specified role only. Equivalent to g_user.hasRoleExactly('itil');

console.log(currentUser.hasRoleExactly('itil'));

//To check current user has one of specified roles. Equivalent to g_user.hasRoleFromList('itil, maint');

console.log(currentUser.hasRoleFromList('itil, maint'));

//To check current user has any roles. Equivalent to g_user.hasRoles();

console.log(currentUser.hasRoles());

//To Get a session client value . Equivalent to g_user.getClientData();

currentUser.getClientData('loginlanguage');

//To get title

console.log(currentUser.title);

//To get emailid

console.log(currentUser.email);

//To get link to user avatar

console.log(currentUser.avatar);

//To clone currentUser object

var cloneCurrentUser = currentUser.clone();

console.log(cloneCurrentUser.userID);

});

}

Here is the output of our above client script.

image

Now you know how to use glideUserSession to get user information instead of slower GlideRecord queries.

View original source

https://www.servicenow.com/community/developer-blog/glideuser-g-user-in-serviceportal/ba-p/2270114