[SalesForce] SOQL query for all user profiles in permission sets

I'm looking to find all names of profiles in a certain permission set. Preferably, I would like to do this in one SOQL query.

For example, I have permission set 1 and in 1, there are 5 users with profiles:

A, A, B, C, D

respectively.

I want to see a SOQL query that will summarize that in permission set 1, there are profiles A, B, C, and D.

This is what I have so far:

SELECT Assignee.Id, Assignee.Name, Assignee.Profile.Name, PermissionSet.Id, PermissionSet.Name 
FROM Profile 
WHERE PermissionSetId IN 
(SELECT Id FROM PermissionSet WHERE Id=PERMSET1 OR Id = PERMSET2 OR Id = PERMSET3 OR Id = PERMSET4)

When I run this in Developer Console, I see that Assignee.Profile returns an [object Object] and I can't see the column with Assignee.Profile.Name. Also, I'm stuck at this step and I don't know how to group all profile names together.

Best Answer

To get around the [object Object] limitation of the Developer Console, use GROUP BY. The following query should get you started.

SELECT PermissionSet.Name PermissionSetName, Assignee.Profile.Name UserProfile, COUNT(AssigneeeId)
FROM PermissionSetAssignment
WHERE ...
GROUP BY CUBE (PermissionSet.Name, Assignee.Profile.Name)
ORDER BY PermissionSet.Name DESC NULLS LAST, Assignee.Profile.Name DESC NULLS LAST

This particular query is designed to show you the same output you'd see in a matrix report, showing each permission set by name, followed by profile, followed by a summary row for each permission set name, eventually followed by the totals per profile, and a grand total at the bottom.

Or, consider using the Data Loader, dataloader.io, workbench, or basically any "real" query application. The Developer Console's Query Execution Planner is particularly useful, but the query output windows are not incredibly useful except in trivial cases, since they can't show child relationships and can't show grandparent or farther parent relationships.