- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2024 08:15 AM
how to get the current logged user has role or not in client controller script widget in servicenow
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2024 09:49 AM
Hello @saradhi1098 ,
In client controller use below script
(function() {
/* widget controller */
var c = this;
// Call server to check if user has role
c.server.get().then(function(response) {
c.userHasRole = response.data.userHasRole;
});
})();
in server script add this
(function() {
/* populate the 'data' object */
/* e.g., data.table = $sp.getValue('table'); */
data.userHasRole = gs.hasRole('admin');
})();
This code will check if the logged in user has the 'admin' role. If the user has the role, userHasRole will be true, otherwise it will be false.
Kindly mark the answer ✔️Correct or Helpful ✔️If it addresses your concern.
Regards,
Siddhesh

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2024 08:42 AM
@saradhi1098 Here is the code you can use to check the user role in client controller script inside a widget.
function($scope, spUtil, $window, glideUserSession) {
/* widget controller */
var c = this;
glideUserSession.loadCurrentUser().then(function(currentUser) {
if(currentUser.hasRole('admin')){
//Do somethig
}else{
//Do something else
}
});
}
For more information on this topic, please refer to https://www.servicenow.com/community/developer-blog/glideuser-g-user-in-serviceportal/ba-p/2270114
Hope this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2024 09:49 AM
Hello @saradhi1098 ,
In client controller use below script
(function() {
/* widget controller */
var c = this;
// Call server to check if user has role
c.server.get().then(function(response) {
c.userHasRole = response.data.userHasRole;
});
})();
in server script add this
(function() {
/* populate the 'data' object */
/* e.g., data.table = $sp.getValue('table'); */
data.userHasRole = gs.hasRole('admin');
})();
This code will check if the logged in user has the 'admin' role. If the user has the role, userHasRole will be true, otherwise it will be false.
Kindly mark the answer ✔️Correct or Helpful ✔️If it addresses your concern.
Regards,
Siddhesh