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
Amit Visapurkar 5Amit Visapurkar 5 

Test class for Chatter CollaborationGroup

trigger ChatterGroupM on CollaborationGroup (after insert) {

       for(CollaborationGroup e:Trigger.New){

        List<User> users=[select id, Username from User where CompanyName='Bodhtree' AND IsActive=true];   
        e = [select id, OwnerId,Name from CollaborationGroup where Id=:Trigger.New];
        List<CollaborationGroupMember> ChatterM = new List<CollaborationGroupMember>();
        //List<CollaborationGroupMember> ChatterM = [SELECT MemberId FROM CollaborationGroupMember WHERE CollaborationGroupId='0F9m00000004K33'];
         for(User user:users){

          system.debug('The Group Name is ' + e.Name);

         CollaborationGroupMember CollMember= new CollaborationGroupMember(,MemberId = user.Id);
         system.debug('The memberId is ' + CollMember.MemberId);
         system.debug('The ownerId is ' + e.OwnerId);
         system.debug('The users are ' + ChatterM);

        insert ChatterM;


How to write the test class for the CollaborationGroup. Its urgent
NagendraNagendra (Salesforce Developers) 
Hi Amit,

Sincerely regret the inconvenience for the delayed reply.

Already this query has a solution on the stack exchange with same issue.

Hope this helps.

Kindly mark this thread as solved if it's resolved.

Best Regards,

Did you get any ideas on this. Please response i am also stuck in same situation. 

I am able to insert CollaborationGroup but whenever I am trying to insert CollaborationGroupMember in it, it is showing insufficient_Error from admin user. 

Rahul Chauhan 33Rahul Chauhan 33

Use this code 


CollaborationGroup groupe = new CollaborationGroup(Name = 'Test1', CollaborationType = 'Public');
        insert groupe;
        Profile prof = [select id from profile where name LIKE '%System%'];

        User u = new User(Alias = 'sd', Email='', 
                          EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
                          LocaleSidKey='en_US', ProfileId = prof.Id, 
                          TimeZoneSidKey='America/Los_Angeles', UserName='');
        insert u;

        collaborationgroupmember cgm = new collaborationgroupmember();
        cgm.CollaborationGroupId = groupe.Id;
        cgm.MemberId = u.Id;
        cgm.CollaborationRole = 'Standard';
        insert cgm;