Find Active, Inactive, total users in all public group across Salesforce Org
If you have come across requirement, where you have to do clean-up of groups and you have no idea where to start.
Final Outcome will be like this
You can prepare a list of Groups and check the count of Total, Inactive, Active users in those groups and reach some conclusion.
I have prepared an apex code script, which does the same job and the final output is CSV file text which you can copy and paste in CSV file and get the final result.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List < Group > lstGroupReg = [Select Id from Group where Type = 'Regular']; | |
List < User > lstInActiveUser = [Select Id from user where IsActive = false]; | |
Set < id > SetInactiveUser = new Set < id > (); | |
for (User objUser: lstInActiveUser) { | |
SetInactiveUser.add(objUser.id); | |
} | |
List < GroupMember > lstGM = [Select Id, UserOrGroupId, GroupId from GroupMember where GroupId IN: lstGroupReg]; | |
//system.debug(lstGM.size()); | |
map < String, Integer > mapGroupToInActive = new Map < String, Integer > (); | |
map < String, Integer > mapGroupTotal = new Map < String, Integer > (); | |
for (GroupMember objGM: lstGM) { | |
if (SetInactiveUser.contains(objGM.UserOrGroupId)) { | |
if (mapGroupToInActive.containsKey(objGM.GroupId)) | |
mapGroupToInActive.put(objGM.GroupId, mapGroupToInActive.get(objGM.GroupId) + 1); | |
else | |
mapGroupToInActive.put(objGM.GroupId, 1); | |
} else { | |
mapGroupToInActive.put(objGM.GroupId, 0); | |
} | |
if (mapGroupTotal.containsKey(objGM.GroupId)) | |
mapGroupTotal.put(objGM.GroupId, mapGroupTotal.get(objGM.GroupId) + 1); | |
else | |
mapGroupTotal.put(objGM.GroupId, 1); | |
} | |
String csvHeader = 'GroupId total inactive \n'; | |
List < String > csvRows = new List < String > (); | |
for (String str: mapGroupTotal.keyset()) { | |
csvRows.add(str + ' ' + mapGroupTotal.get(str) + ' ' + mapGroupToInActive.get(str)); | |
} | |
String CsvFiletotal = csvHeader + String.join(csvRows, '\n'); | |
system.debug(':::::::'); | |
system.debug(CsvFiletotal); |
output String ::
GroupId total inactive 00G2v000004FWzCEAW 1 1 00G90000001mENyEAM 2 1 00G90000001mEO3EAM 1 1
GroupId | total | inactive | Active(do csv formula) |
00G2v000004FWzCEAW | 1 | 1 | 0 |
00G2v000004FWzCEAM | 2 | 1 | 1 |
00G90000001mEO3EAM | 1 | 1 | 0 |
Comments
Post a Comment