function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DeptonDepton 

Empty roles and empty profiles quick view?

Hi,

 

 

Is there any easy way to check which profiles have not any user assigned. the same with the role hierarchy?

 

 

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
Ankit AroraAnkit Arora

You want this result from Apex code or from native screens?

 

If native screen then Setup > Administrator Setup > Manage Users > Profile > Click on your profile > Click on Assigned users.

 

If you want this from Apex code then here it is : (Code is not best optimized as it is created in 10 mins :) )

 

List<User> uList = [select id,ProfileId from User] ;
List<Profile> pList = [select id,name from profile] ;
List<Profile> profileWithNoUser = new List<Profile>() ;
for(Profile p : pList)
{
            boolean flag = false ;
            for(User u : uList)
            {
                     if(u.ProfileId == p.Id)
                     {
                          flag = true ;
                     }    
            }
           if(!flag)
           {
                   profileWithNoUser.add(p) ;
           }
}
System.debug('profileWithNoUser ::: ' + profileWithNoUser + ' ::: ' + profileWithNoUser.size() ) ;

 

Now role hierarchy : Please visit my latest blog post where on visualforce page I have displayed all users falling under each role. Unmanaged package is also there, so you have the code too :)

 

http://forceguru.blogspot.com/2011/12/displaying-role-hierarchy-on.html

 

And here is the working demo : http://treeview-developer-edition.ap1.force.com/

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

All Answers

Ankit AroraAnkit Arora

You want this result from Apex code or from native screens?

 

If native screen then Setup > Administrator Setup > Manage Users > Profile > Click on your profile > Click on Assigned users.

 

If you want this from Apex code then here it is : (Code is not best optimized as it is created in 10 mins :) )

 

List<User> uList = [select id,ProfileId from User] ;
List<Profile> pList = [select id,name from profile] ;
List<Profile> profileWithNoUser = new List<Profile>() ;
for(Profile p : pList)
{
            boolean flag = false ;
            for(User u : uList)
            {
                     if(u.ProfileId == p.Id)
                     {
                          flag = true ;
                     }    
            }
           if(!flag)
           {
                   profileWithNoUser.add(p) ;
           }
}
System.debug('profileWithNoUser ::: ' + profileWithNoUser + ' ::: ' + profileWithNoUser.size() ) ;

 

Now role hierarchy : Please visit my latest blog post where on visualforce page I have displayed all users falling under each role. Unmanaged package is also there, so you have the code too :)

 

http://forceguru.blogspot.com/2011/12/displaying-role-hierarchy-on.html

 

And here is the working demo : http://treeview-developer-edition.ap1.force.com/

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

This was selected as the best answer
DeptonDepton

Hi Ankit,

 

Thank you for your reply.

 

Having more than 50 roles and profiles. I would like an easy way (report) saying how many users are assigned to each role/profile.

 

Currently I have to do it in the native way you mentioned: Profile > Click on your profile > Click on Assigned users, for each of the profiles.

 

Would be perfect to see in a glance if I have roles and profiles that can be deleted!

 

Thank you for your help!!!!

DeptonDepton

Your code works for the role hieratchy!! what about the Profiles?

 

:))

SachiiSachii

You can run 'Summary' reports on User object.

 

User-Role summary report:

Select report type as Users< from Administrative Reports>-->choose format as Summary--> First group by Role --> Then sub-group by Active flag.--> Run Report and Click Hide Details.

 

User-Profile summary report:

Select report type as Users< from Administrative Reports>--> choose format as Summary--> First group by Profile --> Then sub-group by Active flag.--> Run Report and Click Hide Details.