You might take a look at SOQL polymorphism.
Since the parent object referenced by UserOrGroupId could be either a User or a Group, normal SOQL won't let you reference any parent fields, similar to attempting to reference fields via WhatId on an Event. SOQL's TYPEOF keyword should allow you to perform the query you want.
REVISED
The TYPEOF keyword would still give you the query you want, but I apologize for overlooking it's general unavailability. Without this being a widely available feature, I do not think there is another way to create the single query on GroupMember.
...
Map<Id, Group> groupMap = new Map<Id, Group>([
Select Id, Name
From Group
Where Type = 'Queue'
]);
List<GroupMember> groupMembers = [
Select GroupId, UserOrGroupId
From GroupMember
Where GroupId In :groupMap.keySet()
];
Set<Id> userOrGroupIds = new Set<Id>();
for (GroupMember member : groupMembers) {
userOrGroupIds.add(member.UserOrGroupId);
}
Map<Id, User> userMap = new Map<Id, User>([
Select Id, LastName
From User
Where Id In :userOrGroupIds
]);
Map<Id, List<User>> groupIdToUsers = new Map<Id, List<User>>();
for (Id groupId : groupMap.keySet()) {
groupIdToUsers.put(groupId, new List<User>());
}
for (GroupMember member : groupMembers) {
if (userMap.containsKey(member.UserOrGroupId)) {
groupIdToUsersMap.get(member.GroupId).add(userMap.get(member.UserOrGroupId));
}
}
...
With this, you can loop over the values of groupMap and pull the list of Users from groupIdToUsers.
You can alter your filters and limits in the queries as you see fit, especially if you are expecting a large number of Users. There are also certainly other ways you could write this.
If you want to track which Groups are also members of other Groups, you would need to maintain a separate Group map that gets processed similarly to User map.
I observed the same issue in our full sandbox after a refresh. I believe it is a bug, though it is not yet on Salesforce's known issues list.
If you change your query to Select Id,Name from CaseTeamRole where Name != 'CSO'
, your query will probably return all results except the record where the Name is CSO.
If you change your query to Select Id,Name from CaseTeamRole where Name like '%s%'
, your query will probably return two records: CSO and Global Sales Manager.
It looks to me that the results only return if > 1 row is in the result set.
The workaround that could work for you is to query for all CaseTeamRole records and then loop through each result looking for Name = 'CSO'. For example:
for (CaseTeamRole ctr: [select id, name from CaseTeamRole])
{
if(ctr.Name == 'CSO')
{..do stuff.. }
}
If you happened to open a case with Salesforce, would be curious to know if they confirmed whether it is a bug or not.
Best Answer
You can access the name of the owner using
Owner
as the name of the relationship between the objects.