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
SFDC9999SFDC9999 

Test Coverage issue for Collaboration Group (Auto add users to group)

HI ,

I am having a small issue with Coverage on a class , little help please . I am using this code to auto add users to public group

Here is the code
 
trigger Add2Publicgroup on User (after insert) {
   AddUser2CommunityPublicGroup.AddToGroups(trigger.newMap.keySet());

}
public class AddUser2CommunityPublicGroup{

@future
public static void AddToGroups(Set<Id> userIds)
{
 //Get the groups that the user should be added to
Group g=[select Id,Name from Group Where Name='Community Users '];

 List<User> users=[Select Id,Name from user Where Id IN :userIds and (Profileid = '00eM0000000Dyrp')];
 
 List<GroupMember>listGroupMember =new List<GroupMember>();  
 // loop the users that have been created
 for (User user : users){
      GroupMember gm= new GroupMember();   // These are the lines that need coverage 
      gm.GroupId=g.id;
      gm.UserOrGroupId = user.id;
      listGroupMember.add(gm);   
 } 
 insert listGroupMember;
}
}
 
@isTest
private class TestAddUser2CommunityPublicGroup {
static testMethod void TestAddUser2CommunityPublicGroup (){
        Profile profile = [select Id from Profile where name = 'Standard User'];
        
        
        List<User> userList = new List<User>{};
        
            for(Integer i = 0; i < 200; i++){
                User u = new User (FirstName = 'Joe' + i);
                          u.LastName = 'Smith' + i;
                          u.Email = 'joe.smith@xxi.com' + i;
                          u.EmailEncodingKey = 'UTF-8';
                          u.LanguageLocaleKey = 'en_US';
                          u.LocaleSidKey = 'en_US';
                          u.CommunityNickname = 'Jjsmith' + i;
                          u.TimeZoneSidKey = 'America/Los_Angeles';
                          u.ProfileId= profile.Id;
                          u.Alias = 'Jsmith';
                          u.Username = 'jjsmith@constantcontact.com' + i;
                          u.CompanyName = 'xxxI';           
                         
                userList.add(u);
            }                       

        test.startTest();        
        insert userList;        
        test.stopTest();

        List<CollaborationGroupMember> cgm = [SELECT id FROM CollaborationGroupMember 
                                                WHERE CollaborationGroup.Name='Community Users'  AND MemberId IN : userList];
            for (CollaborationGroupMember m: cgm ){
                System.assertequals(200,cgm.size());

            }

    }
    }


GroupMember gm= new GroupMember(); 
      gm.GroupId=g.id;
      gm.UserOrGroupId = user.id;
      listGroupMember.add(gm);   

 These are the lines that need coverage .


Thanks 
G
Tim May 12Tim May 12
Just in case, anyone comes across this post. The issue is that CollaborationGroupMember is a Chatter group and not a Public Group. Adding a user to a Public Group does not add them to a Chatter Group.

https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_groupmember.htm
versus
https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_collaborationgroupmember.htm