You need to sign in to do that
Don't have an account?
LearnSFDC
To get Users belonging to a praticular public Group
Hi,
I seem to be missing something here.I am looking at the API for the group object and there is no method to get the Users belonging to that public group.Could anybody please let me know how to go about it.
This isn't quite as straightforward as it sounds, as groups can contain other groups.
Here's an example that recursively extracts all users from the group and subgroups. It will blow governor limits if the nesting goes too deep:
All Answers
This isn't quite as straightforward as it sounds, as groups can contain other groups.
Here's an example that recursively extracts all users from the group and subgroups. It will blow governor limits if the nesting goes too deep:
Try out the trigger code given below for creating a group and assigned user to them :
trigger sec_CreateGrp on User (after insert)
{
try
{
ID GrpId;
String strGrpName;
Integer sz = Trigger.new.size();
for(integer i=0; i< sz; i++ )
{
User usr = Trigger.new[i];
Id prflId = usr.ProfileId;
Id conId = usr.ContactId;
Profile[] prfNm = [select Name from Profile where Id =: prflId];
//system.debug('XXX KKK ' + prfNm[0].Name );
if(prfNm[0].Name == 'Partner Staff')
{
Contact[] con = [select AccountId from contact where Id =: conId];
strGrpName = 'Z_' + con[0].AccountId;
Group[] grpRec = [Select g.Name, g.Id, g.Email From Group g where g.Name =: strGrpName limit 1];
if(grpRec.size() == 0)
{
Group grp= new Group();
grp.Name = strGrpName;
insert grp;
GrpId = grp.Id;
}
else
{
GrpId = grpRec[0].Id;
}
GroupMember grpMr = new GroupMember();
grpMr.UserOrGroupId = usr.Id;
grpMr.GroupId = GrpId;
insert grpMr;
}
}
}
catch (Exception e) {system.debug('Exeption -> ' + e );}
}
Hope this helps.
Thank you bob and Pradeep for the replies. My requirement was not so complex as all the members of the group were users and i wanted to fetch one of the user's belonging to that group.Bob's post gave me the solution i was looking for.I ended up with a simple query as below
Id userId=[Select g.UserOrGroupId From GroupMember g where g.Group.name = 'xyz' limit 1].UserOrGroupId
Just in case anyone stumbles on this page in future, I believe I've improved on @bob_buzzard's version:
Basically, @bob_buzzard's version will result in a query for every group within the group, whereas the above version will only result in one query per nesting level.
Thanks to @bob_buzzard for breaking the back of it, and hope it helps someone!
Matt
I can't seem to get the above (or Bob's original) to work. I keep getting this error:
Error: Compile Error: Unexpected token 'Set'. at line 1 column 15
Any ideas?
The code I (and Bob) posted is just a method, it needs to be part of a class. The class can be anything you want, I personally have the method in a class called Utils.
So the whole thing would go like this:
public class Utils {
public static Set<Id> getUsersFromGroupIds(Set<Id> groupIds) {
<rest of method>
}
}
Hope that helps!