+ Start a Discussion

Error [statusCode=INVALID_CROSS_REFERENCE_KEY ] while adding a user to a chatter group

I am trying to add user to a chatter group from apex. I am getting an error

    "Error [statusCode=INVALID_CROSS_REFERENCE_KEY, code=[xmlrpc=1202, statusCode=INVALID_CROSS_REFERENCE_KEY, exceptionCode=INVALID_CROSS_REFERENCE_KEY, scope=PublicApi, http=400], message=This user can't be added to the group., fields=null]"


Here is the code i am using


    lstCGM.add(new CollaborationGroupMember(CollaborationGroupId = lCGA[k].Chatter_Group_ID__c ,
                                                            // CollaborationRole = 'Member',
                                                            MemberId = u[i].Id    ));

        for(CollaborationGroupMember cgmr : lstCGM)
            system.debug('Processing the CGM record ' + cgmr);
        if (lstCGM.size() > 0)
               // Insert the CGM Records
            Database.SaveResult[] lsr = Database.insert(lstCGM, false);
            // Loop through the save results writing errors to the debug log
            for (Database.SaveResult sr : lsr)
                     Database.Error err = sr.getErrors()[0];
             }  // END LOOP through the save results writing errors to the debug log
         }// END IF there are records to insert, insert them.


The debug logs return the correct ids


  Processing the CGM record CollaborationGroupMember:{CollaborationGroupId=0F9E00000008cpwKAA, MemberId=005c0000000R2kQAAS}
    USER_DEBUG|[275]|DEBUG|This user can't be added to the group.


Checked the ids on the browser and they were valid.
Any idea what should be causing this?

Jia HuJia Hu
The error means the User you added can't access this Group.

What is the Profile of your User, for example, if it is 'Chatter External User', you will get exactly the same error.