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.
Build the map like this:
id oppId = Id.valueOf(String.valueOf(OLIPlays.get('OpportunityId')));
integer CountofType = integer.ValueOF(OLIPLays.get('expr0'));
string NameofType = string.valueOf(OLIPlays.get('Play__c'));
if( !opptyPlayCount.containsKey( oppId ) )
{
opptyPlayCount.put( oppId , new Map< string, integer >( ));
}
if( !opptyPlayCount.get( oppId ).containsKey( NameofType ))
{
opptyPlayCount.get( oppId ).put( NameofType, integer( ));
}
opptyPlayCount.get( oppId ).get( NameofType ).add( CountofType );
Then retrieve the value like this:
if(opptyPlayCount.containsKey(opptyId)){
if(opptyPlayCount.get(opptyId).containsKey('Red')){
isRed = opptyPlayCount.get(opptyId).get('Red');
}
}
Best Answer
If you want to count how many records match each
Application_Prefix__c
, then you should removeId
from yourSELECT
clause:You can't really get both the individual Id values and the count per prefix in a single query. If you wanted to get the counts using post-processing, you could do a
Map<String, List<Test__c>>
or even just aMap<String, Integer>
. Here is a common grouping pattern: