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.

//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);
view raw gistfile1.java hosted with ❤ by GitHub

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

Popular posts from this blog

How to change/update the profile of a User in Salesforce from apex code

Deep Dive into Salesforce to Salesforce Connection

File Migration from One Salesforce Org to Another Org using Skyvia