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
Erin Rico-Allen 2Erin Rico-Allen 2 

Trying to Write a SQL Query in Workbench to find all Roles that don't have users in them

Hi,
We have met our max on roles created in our Salesforce org. I need to be able to pull all roles that currently aren't being used (no users are listed in them). Trying to write a SQL Query to plug into Workbench in order to get the role id, last modified date, name, etc. Can someone help me?

Thanks!
Best Answer chosen by Erin Rico-Allen 2
Raj VakatiRaj Vakati
Use this code in execute window on developer console  . i Dnt think so simple SOQL will works 

Refer this link

https://salesforce.stackexchange.com/questions/92742/report-to-get-all-roles-in-salesforce-with-assigned-users-and-without-users
 
Map<Id, UserRole> roles = new Map<Id, UserRole>([SELECT Id, Name, DeveloperName FROM UserRole]);

List<User> users = [SELECT Id, UserRoleId FROM User];

List<String> rolesWithoutUsers = new List<String>();

for(User theUser : users)
{
    roles.remove(theUser.UserRoleId);
}

for(UserRole theRole : roles.values())
{
    rolesWithoutUsers.add(theRole.Name);
}

system.debug(rolesWithoutUsers);