function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
s_macs_mac 

collaboration group member

can any body please help me with,how to add members to a collaboration group using trigger..

 

Any help would be helpfull..

 

Thanks in Advance!!!

jkucerajkucera

Sure - its something like this:

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

3) Add them all as new CollaborationGroupMember's