How to find Active users count, Inactive users count and whether users are assigned to the profile in Salesforce?

1. Execute the below code in Developer Console.

  1. for ( Profile objProfile : [ SELECT Name, ( SELECT Id, IsActive FROM Users ) FROM Profile ORDER BY Name ] ) {  
  2.       
  3.     Integer activeCount = 0;  
  4.     Integer inactiveCount = 0;  
  5.       
  6.     for ( User objUser : objProfile.Users ) {  
  7.           
  8.         if ( objUser.IsActive )  
  9.             activeCount += 1;  
  10.         else   
  11.             inactiveCount += 1;  
  12.           
  13.     }  
  14.       
  15.     system.debug( objProfile.Name + ',' + activeCount + ',' + inactiveCount + ',' + ( ( activeCount > 0 || inactiveCount > 0 ) ? 'Have Users' : 'No Users assigned' ) );  
  16.       
  17. }  

2. Open the log.

3. Select Debug Only.

4. Press CTRL + A.


5. Paste it in a excel file.

6. Copy only the Debug statements.

7. Paste it in a note pad file.

8. Replace [15]|DEBUG| with blank.

9. Save the file with .csv extension.

No comments:

Post a Comment