Find Active, Inactive and Total users per Role vise across salesforce Org
Below script will be useful when we have to find out Active/Inactive/Total
users across sf org and No of roles are so many, so doing thing manually won't work.
I have prepared below apex script which can be execute in developer console or
workbench execute anonymous window.
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
//Find use of Role across active/inactive users | |
List < User > lstUsers = [Select Id, IsActive, ProfileId, UserRoleId from user where UserRoleId != null]; | |
map < String, Integer > mapGroupToInActive = new Map < String, Integer > (); | |
map < String, Integer > mapGroupTotal = new Map < String, Integer > (); | |
for (User objUser: lstUsers) { | |
if (!objUser.IsActive) { | |
if (mapGroupToInActive.containsKey(objUser.UserRoleId)) | |
mapGroupToInActive.put(objUser.UserRoleId, mapGroupToInActive.get(objUser.UserRoleId) + 1); | |
else | |
mapGroupToInActive.put(objUser.UserRoleId, 1); | |
} | |
if (mapGroupTotal.containsKey(objUser.UserRoleId)) | |
mapGroupTotal.put(objUser.UserRoleId, mapGroupTotal.get(objUser.UserRoleId) + 1); | |
else | |
mapGroupTotal.put(objUser.UserRoleId, 1); | |
} | |
String csvHeader = 'RoleId 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); |
I hope this will be useful for you.
Output will be like this when copied in csv file.
RoleId | total | inactive |
00E900000016i8gEAA | 1 | null |
00E900000016i8oEA | 1 | 1 |
Comments
Post a Comment