1

I need a report that shows all roles. Currently I have created a report on user which shows those roles which have users assigned, but I need to show all roles with or without users assigned.

1
  • Thanks for the below Solutions.But still if i need to show report with roles (assigned to active users and without users) then what is the solution to achieve this functionality. Sep 23 '15 at 4:24
4

You can't run a report against the UserRole object but you can try this instead:

  1. Go into the Developer Console and select the SOQL query tab or you can login to workbench.developerforce.com.

  2. Go to the Queries Menu/SOQL Query

  3. Paste this query in the query box and click the Execute (or Query) button.

This query selects all roles that are not found in the User table.

SELECT Id, Name
FROM UserRole
WHERE Id NOT IN (SELECT UserRoleId FROM User WHERE UserRoleId !='000000000000000')

This is what you get:

enter image description here

You can then run the same query a bit modified to get all Roles that are assigned

SELECT Id, Name
FROM UserRole
WHERE Id IN (SELECT UserRoleId FROM User WHERE UserRoleId !='000000000000000')

The results can be placed in excel and then imported to a custom object where you can create a report on it if you wanted.

4
  • Appriciate the early Response. Sep 18 '15 at 9:38
  • Thanks very much.Here (id=0000000000000000)?.does it mean blank value Sep 18 '15 at 9:39
  • Yes. If you liked the answer, why not express it by voting for it. I hope that it helped.
    – user24509
    Sep 20 '15 at 15:17
  • yes.Sure here is the vote. Sep 22 '15 at 4:17
0

I don't think you can do this with a report, but you could use something like the following in an execute anonymous window, which would give you an output with the names of the roles not being used.
You may want to add in criteria to only look at active users, or maybe remove community/partner 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);
0
0

If you want to use the rest api the endpoint you can use user24509 response with /services/data/v37.0/queryAll?q=SELECT Id, Name FROM UserRole WHERE Id IN (SELECT UserRoleId FROM User WHERE UserRoleId !='000000000000000')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.