Want to know how Can I know the users who is not a member of any group?

ahmed21
Kilo Expert

Hello ServiceNow community, I have a requirement to know who from the active users we have is not a member of a group with type "ITIL" (Type is an out of the box field on the group table.

we are on Helsinki Patch 11

Thanks a lot.

1 ACCEPTED SOLUTION

Hi Ahmed,




You can pull all users who are not mapped to any group using the following method.




1. Create a script include and place the below function in it.


script:


*************************


find_real_file.png



*****************************



2. Create a report on 'sys_user' table and call the script include function in the report filter. Please refer the below screenshot. On running the report, you'll get the list of users who are not member of any group.




find_real_file.png





Similarly, you can use the below script to fetch the list of users who are member of non ITIL type group.


find_real_file.png



-Udhay


Please Hit like, Helpful or Correct depending on the impact of the response


View solution in original post

22 REPLIES 22

Hi Ahmed,




You have incorrectly named your script include. Hence report didn't fetch you any result. Please follow the below steps to create script include




Script Include:


Name: userMembership


Client Callable: True


Script:


var userMembership = Class.create();


userMembership.prototype = Object.extendsObject(AbstractAjaxProcessor, {




fn_nonGrpMembers:function(){


var userStr = '';


var gr = new GlideRecord('sys_user_grmember');


gr.addQuery('user.active',true);


gr.query();



while(gr.next()){



if(userStr.indexOf(gr.user) == -1){


userStr = userStr+','+gr.user;


}



}


userStr = userStr.slice(1);



var result = '';


var gr1 = new GlideRecord('sys_user');


gr1.addQuery('sys_id','NOT IN',userStr);


gr1.query();



while(gr1.next()){


result = result+','+gr1.sys_id;


}


result = result.slice(1);



return result;


},



fn_nonITILMembers: function(){


var grpTypeSysId = '1cb8ab9bff500200158bffffffffff62';//sys id of the group type itil


var nonITILUserStr='';



var gr = new GlideRecord('sys_user_grmember');


gr.addEncodedQuery('group.typeNOT LIKE'+grpTypeSysId+'^ORgroup.typeISEMPTY');


gr.query();



while(gr.next()){



if(nonITILUserStr.indexOf(gr.user) == -1){


nonITILUserStr = nonITILUserStr+','+gr.user;


}



}



nonITILUserStr = nonITILUserStr.slice(1);


return nonITILUserStr;


},



      type: 'userMembership'


});






-Udhay


Please Hit like, Helpful or Correct depending on the impact of the response


Hi Ahmed,
You have incorrectly named your script include. Hence report didn't fetch you any result. Please follow the below steps to create script include
Script Include:Name: userMembershipClient Callable: TrueScript:var userMembership = Class.create();userMembership.prototype = Object.extendsObject(AbstractAjaxProcessor, {
fn_nonGrpMembers:function(){ var userStr = ''; var gr = new GlideRecord('sys_user_grmember'); gr.addQuery('user.active',true); gr.query(); while(gr.next()){ if(userStr.indexOf(gr.user) == -1){ userStr = userStr+','+gr.user; } } userStr = userStr.slice(1); var result = ''; var gr1 = new GlideRecord('sys_user'); gr1.addQuery('sys_id','NOT IN',userStr); gr1.query(); while(gr1.next()){ result = result+','+gr1.sys_id; } result = result.slice(1); return result; }, fn_nonITILMembers: function(){ var grpTypeSysId = '1cb8ab9bff500200158bffffffffff62';//sys id of the group type itil var nonITILUserStr=''; var gr = new GlideRecord('sys_user_grmember'); gr.addEncodedQuery('group.typeNOT LIKE'+grpTypeSysId+'^ORgroup.typeISEMPTY'); gr.query(); while(gr.next()){ if(nonITILUserStr.indexOf(gr.user) == -1){ nonITILUserStr = nonITILUserStr+','+gr.user; } } nonITILUserStr = nonITILUserStr.slice(1); return nonITILUserStr; },       type: 'userMembership'});

-UdhayPlease Hit like, Helpful or Correct depending on the impact of the response


Thank you Udhay,


It works perfectly, I just fining an issue defending the type of the group that the user is a member of. I don't know if that is possible?


If you are expecting to change the group type then you can change the group type by modifying the variable given below with appropriate sys id.


var grpTypeSysId = '1cb8ab9bff500200158bffffffffff62';//sys id of the group type itil



To find the sysid of the group types navigate to   sys_user_group_type.LIST. This will provide the list of group types from which you can copy the desired group type's sysid and replace it in the above code.



-Udhay


Please Hit like, Helpful or Correct depending on the impact of the response. Thanks!


Dave Smith1
ServiceNow Employee
ServiceNow Employee

I have a requirement to know who from the active users we have is not a member of a group with type "ITIL" (Type is an out of the box field on the group table.


System Security > Reports > Role Allocation will tell you who holds the "itil" role.



It won't tell you how they obtained it (which group) without drilling-down, and it won't specify if they're active (unless you simply mean the "active" checkbox).


Thanks, Dave,


The idea is we have to maintain all of the users to a part of a group, regardless this group has a role or not.


because our workflow approvals based on the group stractuer.