isMemberOf is not working

sainath3
Mega Guru

Hi Everyone,

 

I have a requirement that, I need to restrict access to one tab in the portal to specific groups(SIT user & UAT user) only,

 

But there are no direct group members in both the groups, there are multiple roles(Testing user role & align mgt) in both the groups.

I want to give access the users who are in both the groups (SIT user & UAT user).

 

Code:

if(gs.getUser().isMemberOf('SIT user') || if(gs.getUser.isMemberOf('UAT User') ){

data.testuser=true

}

 

result: false even the user is part of the role.

 

Can you please suggest me.

 

 

 

 

1 ACCEPTED SOLUTION

@sainath3 

why are you giving user direct role?

Instead give role to group and add user to that group?

if those are not added in group then simply use this to check both the roles

gs.hasRole('role A') && gs.hasRole('roleB')

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

10 REPLIES 10

Vishal Jaswal
Giga Sage

Hello @sainath3 

Try this

if (gs.getUser().isMemberOf('SIT user') || gs.getUser().isMemberOf('UAT User')) {
   data.testuser = true;
}

Hope that helps!

Hi Vishal,

Thanks for the quick response.

I tried the same it is returning false.

 

Actually there no direct group members but, there  are some users in roles under that groups.

I want to give access to only the user who are in roles(Testing user role & align mgt) which is under those groups.

Hello @sainath3 

In ServiceNow, gs.getUser().isMemberOf('Group Name') only checks direct group membership, not roles or nested relationships like users having roles that are assigned to a group.

If you want to check whether a user has a specific role (like Testing user role or Align Mgt), you should check roles directly, not group membership.



if (gs.hasRole('testing_user_role') || gs.hasRole('align_mgt')) {
   data.testuser = true;
}

Hope that helps!

Then it will not work indeed. You can either try Vishal's approach, or target the child groups (parents are evaluated, children are not for isMemberOf()).