+ Start a Discussion
buggs sfdcbuggs sfdc 

Test class for GroupMember

HI 

Can any one help me out in testing the same scenario for delete statement.
Really appreciate your quick responses.

Thanks
trigger userdeactive on User (before insert,before update) {
List<GroupMember> group = new List<GroupMember>([select Id, UserOrGroupId, groupid From GroupMember where groupId IN 
                                                 (SELECT id FROM group WHERE Type = 'Queue') 
                                                  AND UserOrGroupId IN (SELECT id from user WHERE isactive=FALSE)]);

   delete group;            
}
 
@isTest 
private class deActiveUserTest {
    static testMethod void validateremoveInActiveUser() {
       {
 User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];
      System.runAs (thisUser) 
      {        // Insert user
        Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator']; 
        User u = new User(Alias = 'standt', Email='test@test.com', 
        EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
        LocaleSidKey='en_US', ProfileId = p.Id, 
        TimeZoneSidKey='America/Los_Angeles', UserName='sdss123@test.com');
        insert u;
        
        // Insert group
        Group grp = new Group();
        grp.Name = 'Technology Partner Content';
        insert grp;
        
        test.starttest();
         system.assertequals(u,[select id from user WHERE isactive=FALSE]);
         delete grp;
         test.stoptest();
 
        
        // Insert contact with your required field.
        Contact cnt = new Contact();
        cnt.LastName = 'Test contact';
        cnt.Email = 'test@test.com';
        //cnt.Public_Group_Technology_Partner_Content__c = true;
        insert cnt;
        }
    }
}
}
Always ThinkinAlways Thinkin
Do you need to set the Group.Type value to "Queue"? Your query SELECT id FROM group WHERE Type = 'Queue' implies that only Groups with that Type will be returned to the result set you use for deletion. Also, it seems like you would need to assign the Contact to the Group to make this work.
buggs sfdcbuggs sfdc
HI 

A great thanks for quick response,yes i need to set the Group.Type value to "Queue" and delete the inactive users in the group.as a newbie to apex code i just have few questions.
-Here its covering the the trigger 100% with the above test class,but i havent used any delete statment in my test class,so will it work if i push the same code to live.
-And some one suggested me to use system.Assert statements ,i googled it but still i havent got any perfect answer,can you clear me off exactly when we wanted to use system.assert statements.
Always ThinkinAlways Thinkin
OK, let's talk about your trigger. It lacks any reference to the specific user involved in the Before Insert or Before Update, so this means that when ever any user is updated, the code is going to run, get all the inactive Group Members in Queue-type Groups and delete them. I hadn't caught that on first examination since I was focused on the unit tests. Do you really want to clear out all inactive Users from the Queue Groups every time? Or are you just seeking to remove the User that was updated (assumedly to inactive) from any Queue Groups in which the User is a member?

If you are only seeking to remove the User that was just deactivated, you must use a for loop to iterate through all Users that were updated in the transaction to bulkify the trigger. In the for loop you would have a conditional that tests if the update to the User.IsActive field changed from True to False and only include those Users' IDs in the query to find their Group Member records for deletion.

There are lots of ways to use the Assert statements, but what you would most likely want in this unit test is to use at least two. First, before you deactivate the User, you want to do an assertion that the User is actually a member of the Queue Group you created in the test. And second, after the update to the User, you want to assert that it no longer is a member of the Queue Group.

I tend to be very obsessive about my unit tests and asserts and would probably create a second User in the Group and also do an assert to confirm that it remained in the Group after the update to deactivate the first User.