Need help gs.getUser.hasRole not working as exepcted

shaik_irfan
Tera Guru

Hi,

 

I have a Display BR as below

 

g_scratchpad.uirole = gs.getUser().hasRole('ui_role');

 

and in Client scrpt on Load

 

alert(g_scratchpad.uirole);

 

when i impersonate to itil is user it is working perfectly fine but for admin even admin user dont have that role it is dsplaying as 'true'

 

Can anyone please help me it should dispaly as false for admin's too if the user dont have that role

1 ACCEPTED SOLUTION

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Have you also tried g_user.hasRoleExactly('itil');

See example here:
https://developer.servicenow.com/app.do#!/api_doc?v=newyork&id=r_GlideUser-hasRoleExactly_String

Note: this is already about Client Side. So you don't need to do this Server Side, using a Business Rule with scratchpad, and then Client Side.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

View solution in original post

4 REPLIES 4

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Have you also tried g_user.hasRoleExactly('itil');

See example here:
https://developer.servicenow.com/app.do#!/api_doc?v=newyork&id=r_GlideUser-hasRoleExactly_String

Note: this is already about Client Side. So you don't need to do this Server Side, using a Business Rule with scratchpad, and then Client Side.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Harsh Vardhan
Giga Patron

try now

 

 

if (gs.getUser().getRoles().indexOf('ui_role') > -1){

g_scratchpad.uirole = 'true';

}

 

client script:

 

alert(g_scratchpad.uirole);

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Shaik,

that is because admin inherits all the roles;

so even admin user would have that role as it gets inherited

try this to know if user has the exact role

no need for display business rule and scratchpad; in client side itself you can know whether user has exact role or not

function onLoad(){

alert(g_user.hasRoleExactly('ui_role'));

}

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader