To check user is part of the group

gomathyshankar
Tera Contributor

Hi,

In the catelog activity in the work flow   we are adding the requester to the group using the script.I need to add condition to check if user was

added properly to the group.In short I need to verify the requestor is part of that group.

5 REPLIES 5

obama
Kilo Expert

Replace groupName



gs.getUser().isMemberOf(groupName));


snehabinani26
Tera Guru

HI Gomathshankar,



You can validate this with the help of script mentioned below.



var count = 0;


var gr = new GlideRecord("sys_user_grmember") // table that stores the group member details


gr.addQuery("user",requestor);// pass the requestor field column name like current.request.requested_for for RITM table.


gr.addQuery("group",group); // pass in the group sys_id


var count = gr.getRowCount();


if (count > 0){


gs.log("User added");


else {


gs.log("User not added");


}



Hope this helps you.



Regards,


Sneha Binani


Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Gomathyshankar,



You can create "If" activity in the workflow and return true or false output. Script will be


// This script needs to set answer to 'yes' or 'no' to indicate the state of the activity.


//


// For example,




    answer = ifScript();




    function ifScript() {


          if (gs.getUser().isMemberOf(current.variables.PASSGROUPNAME HERE))) { //Replace PASSGROUPNAME HERE with exact name of the catalog variable in case group name is store in variable for variable it will PASSVARIABLENAME.getDisplayValue()


                return 'yes';


          }


          return 'no';


  }









Reference:


http://wiki.servicenow.com/index.php?title=Getting_a_User_Object#gsc.tab=0


Condition Activities - ServiceNow Wiki



Let me know if you have any questions.


johnnyjava
Kilo Guru

If you need to check if the user is a member of at least one of a GlideList of groups, use something like this:



answer = checkGroupsOnComp();



function checkGroupsOnComp(){


  var access_groups = (current.u_component.u_groups.toString()).split(",");


  var users_groups = gs.getUser().getMyGroups().toArray();


  converted_list = [];


  for (var i=0; i < users_groups.length; i++){


  converted_list.push(users_groups[i] +'');


  }


  var au = new ArrayUtil();


  var combined = au.concat(access_groups, converted_list);


  var uniq = au.loose_unique(combined); //loose_unique is a copy of unique but the comparison operator is == not ===


  return (combined.length != uniq.length); //if there's a dupe, they are a member of at least one group


}



loose_unique is sometimes needed since the strings could have different classes.