Skip to content

Instantly share code, notes, and snippets.

@ankushsomani09
Created July 6, 2020 05:04
Show Gist options
  • Save ankushsomani09/18d057ac652472bf6986bffc5189faa2 to your computer and use it in GitHub Desktop.
Save ankushsomani09/18d057ac652472bf6986bffc5189faa2 to your computer and use it in GitHub Desktop.
Find Active, Inactive, Total user in Groups across salesforce org
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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment