+ 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