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
ashish jadhav 9ashish jadhav 9 

How to find those profiles who don't have any users?

How can I find a list of profiles exists in SFDC who don't have any user assigned to them?
NagendraNagendra (Salesforce Developers) 
Hi ashish jadhav 9,

Please find the below explanation how you can find a list of profiles in SFDC who doesn't have any users.

If you read carefully attached are steps 

1. Query for grabbing list of all profiles
2. Query for grabbing list of all user
3. Generate a list of Profile type to collect - 'profiles with no user' 
4. Iterate to all profile - set up a flag as false

 for each profile ( if user.profileId == profile.id) - based on Salesforce schema (if user.profileId matches with original profile id)

5.  else if not - add that profile to our custom list 
6. Display list of profile 

The below code snippet matches your requirement criteria.
//Apex class to generate the list of user assigned to a profile
 
List<User> lUser = [SELECT Id , ProfileId from User ];  // list of users
List<Profile> lProfile = [SELECT Id, ProfileID from Profile];
List<Profile> noUserProfile = [SELECT id, ProfileId from Profile];
 
for(Profile p : lProfile)
{
 
//iterate and figure out fill up the corresponding list
bool flag= false;
 
for(User u : lUser)
{
 if(profileId ==p.id)
   flag=true;
}
if(!flag)   //whenever the flag is true
noUserProfile.add(p);  //add this profile to nouserprofile list
 
}     //end of iteration for profile list
 
 
System.debug('profileWithNoUser ::: ' + profileWithNoUser + ' ::: ' + profileWithNoUser.size() ) ;
 
}

Please mark my solution as best answer if it helps you.

Best Regards,
Nagendra.P