how to get the current logged user has role or not in client controller script widget in servicenow

saradhi1098
Tera Contributor

how to get the current logged user has role or not in client controller script widget in servicenow

1 ACCEPTED SOLUTION

Siddhesh Gawade
Mega Sage
Mega Sage

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

 

View solution in original post

2 REPLIES 2

Sandeep Rajput
Tera Patron
Tera Patron

@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.

Siddhesh Gawade
Mega Sage
Mega Sage

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