1

I have something wrong with my map containskey filter as it isn't finding the existing user. Maps still tie my mind up so this is probably simple and I am just not spotting what I have done wrong.

 @future
    public static void addChatterGroup(List<ID> UserIds) {

        list<User> userlist = new list<user>(
            [Select Id
             FROM User
             WHERE Id IN :UserIds]);

        list <CollaborationGroupMember> chattergroup = new list <CollaborationGroupMember>();

        //Workaround for Chatter test class issue
        string nm = Test.isRunningTest() ? 'Test All BCInsourcing' : 'All BCInsourcing'; 

        //Find Chatter Group ID
        CollaborationGroup BCIGroup = [Select Id,Name 
                                       FROM CollaborationGroup 
                                       WHERE Name = :nm
                                       LIMIT 1];

        Map<ID, CollaborationGroupMember[]> userToGroups = New Map<ID, CollaborationGroupMember[]>();

        for(CollaborationGroupMember existingMembers :[Select MemberId
                                        FROM CollaborationGroupMember
                                        WHERE CollaborationGroupId = :BCIGroup.Id]){
           CollaborationGroupMember[] tmp = userToGroups.get(existingMembers.MemberId);

           if(tmp == null) tmp = New CollaborationGroupMember[]{};

           tmp.add(existingMembers);
           userToGroups.put(existingMembers.MemberId,tmp);
        }

        for(User u:userlist){
            if(!userToGroups.containsKey(u.Id))
            {
                chattergroup.add(New CollaborationGroupMember( 
                    CollaborationGroupId = BCIGroup.Id,
                    MemberId             = u.Id
                ));   
            }
        }


        Insert chattergroup;

    }

enter image description here

  • Are you sure that userList and existingMembers contains all the data that you are expecting? Can you share what system.debug returns for those two? – kurunve Oct 11 '16 at 19:26
6

You are building a Map of CollaborationGroupMember ID's to CollaborationGroupMembers here:

Map <Id, CollaborationGroupMember> existingMembers = new Map <Id,CollaborationGroupMember>(
                                        [Select MemberId
                                        FROM CollaborationGroupMember
                                        WHERE CollaborationGroupId = :BCIGroup.Id]); 

When you create a Map from an SOQL query the KEY of the Map will be the Record ID of the Object being queried.

So when you attempted the containsKey here:

if(!existingMembers.containsKey(u.Id)){}

it will ALWAYS evaluate to true (does not contain key) as a User ID will never be in a map with CollaborationGroupMember IDs as the key

NOTE: You debug is in the wrong place and the code will not compile

What you want to do is build a Map with User IDs as the keys like so:

Map<ID, CollaborationGroupMember[]> userToGroups = New Map<ID, CollaborationGroupMember[]>();


for(CollaborationGroupMember existingMembers :[Select MemberId
                                        FROM CollaborationGroupMember
                                        WHERE CollaborationGroupId = :BCIGroup.Id]){
   CollaborationGroupMember[] tmp = userToGroups.get(existingMembers.MemberId);

   if(tmp == null) tmp = New CollaborationGroupMember[]{};

   tmp.add(existingMembers);
   userToGroups.put(existingMembers.MemberId,tmp);
}

This will populate a Map by MemberID to a list of CollaborationGroupMember records (assuming a user can have many records). The keys will be user ids. You will have to update the rest of your code to work with lists instead of a concrete sObject

  • I updated my code to include what you gave me, thanks, but it still appears to not work correctly. I updated my first post with my current code. Any ideas? – Matt M Oct 11 '16 at 20:52
  • 1
    @MattM - Define not working, did you do any system debugs? Maybe what you think should happen actually should not happen. "Its not working" does not give enough to work with as it should be working unless expectations are wrong or other code is not doing what you think it is – Eric Oct 11 '16 at 20:53
  • Base Process: I have a trigger that adds users to chatter groups as well as other things when the user goes from inactive to active. Goal: So what i am trying to do is prevent the trigger from trying to add the user to the chatter group again as they are already a member of the group and an insert error will generate. – Matt M Oct 11 '16 at 21:00
  • 1
    @MattM - So what is "Not Working" - What do the queries debug as, what are the values for UserIds, etc, etc. Not working simply is not enough to help you. nm could not have a collaborationgroupmember for that user, the user ids could be wrong, etc. Debug the stuff out, run the queries in the dev console to see what they return, step through the code and find the issue. I bet that a property is not set as you expect, a caught exception could have been thrown, or any one of 1000 other possibilities. The code I posted will work assuming the values you are using to generate the map are correct. – Eric Oct 11 '16 at 21:56
  • I added a debug screen shot. For some reason the user isn't showing up in the query of collaborationgroup. Any ideas why this would be? – Matt M Oct 12 '16 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.