Need a second set of eyes, isMemberOf not working

ServiceNowSteve
Giga Guru

All I am trying to do is see if I am a member of the Managers group (which I am) but this code keeps telling me it's false. Any idea why?

var currentUser = gs.getUser();

if(currentUser.isMemberOf('Managers'))

{

gs.log('true');

}

else

{

gs.log('false');

}

1 ACCEPTED SOLUTION

The first thing that comes to mind is that there may be duplicate groups with the same name. Hopefully that is not the case. If there is, perhaps that is why it is returning false. The query finds the first group with that name and unfortunately, you are not a member of **that** group.



See what happens if you remove yourself from a group, then add yourself back in and retry the script. Hopefully it is not an indexing problem, or you are running your script on the instance where you are not part of that group.



Here is a script you can use to get all of your memberships. I modified it so that it will return the names and the sys_id of the group:


      var u = gs.getUserID();


      var myGroups = getGroupMembership(u);


      gs.info(myGroups);



function getGroupMembership(usrID) {


              var retArray = [];


              var memObj = new GlideRecord('sys_user_grmember');


              memObj.addQuery('user', usrID);


              memObj.query();


              while (memObj.next()) {


                      var groupInfo = memObj.group.name + '[' + memObj.group + ']';


                      retArray.push(groupInfo);


              }


              return retArray;


}


View solution in original post

21 REPLIES 21

Surendra, I completely missed it and didn't realised that it was returning false.


In that case, Steve are you a part of that Managers group?? Trying replacing Managers group name with its sys_id and see if it makes any difference.



Please mark my response as correct and helpful if it helped solved your question.
-Thanks

Surendra Raika1
Kilo Guru

Its crazy ... lol



var currentUser = gs.getUser();


gs.addInfoMessage(currentUser.getDisplayName());


gs.addInfoMessage(currentUser.getFirstName());


gs.addInfoMessage(currentUser.isMemberOf('Managers'));


gs.addInfoMessage(currentUser.isMemberOf('CAB Approval'));



Screen shot of your background script... Results ..


find_real_file.png


sachin_namjoshi
Kilo Patron
Kilo Patron

Please use below code in server scripts ( fix script, business rule...)




 


  1. if(gs.getUser().isMemberOf('<SYS_ID_OF_MANAGERS_GROUP>'))  
  2. {  
  3. gs.log('true');  
  4. }  
  5. else  
  6. {  
  7. gs.log('false');  
  8. }


Regards,


Sachin


find_real_file.png