trigger addMembers on CollaborationGroup (after insert) {
//Lets say you want to add everyone with a Dept = Sales to all new groups
List<user> users=[SELECT Id FROM User WHERE Department='Sales'];
for (CollaborationGroup g:trigger.new()){
//Place to store all the members to add
List<CollaborationGroupMember> gms=new List<CollaborationGroupMember>();
for (User u:users){
CollaborationGroupMember gm=new CollaborationGroupMember();
gm.collaborationGroupId=g.id;
gm.memberId=u.Id;
gm.NotificationFrequency='P';//I forgot what the values are and the API guide isn't loading for me:
//http://www.salesforce.com/us/developer/docs/api/index.htm
gms.add(gm);
}//for 2
}//for 1
try{
insert gms;
}catch (DMLException e){
system.debug('The group members werent added properly. Error: '+e');
}//try
}//trigger addMembers
The gist:
1) Get a list of user Id's
2) Figure out the logic of which new groups to add the users to
Sure - its something like this:
The gist:
1) Get a list of user Id's
2) Figure out the logic of which new groups to add the users to
3) Add them all as new CollaborationGroupMember's