I am trying to add users to the chatter groups based on region field on User object using trigger and apex class.
When ever I am creating or updating a user it is giving Invalid Cross reference key Error
I hardcoded the Id's but still same error !!
Class:
@future
public static void groups(Set<Id> userIds)
{
List<User> users=[select id,UserTheater__c,Name,Username from User where id in :userIds];
List<String> groups=new List<String>{'A','B','C'};
List<CollaborationGroup> chatGps=[select id, Name from CollaborationGroup where name in :groups];
List<CollaborationGroupMember> chatGpMem=new List<CollaborationGroupMember>();
for (User user : users)
{
for (CollaborationGroup chatterGroup : chatGps)
{
CollaborationGroupMember c = new CollaborationGroupMember();
if(user.reg__c == 'TL' && chatterGroup.name == 'A' ){
c.CollaborationGroupId=chatterGroup.id;
c.MemberId = user.Id;
}
else if(user.reg__c == 'DL' && chatterGroup.name == 'B' ){
c.CollaborationGroupId=chatterGroup.id;
c.MemberId = user.Id;
}
else if(user.reg__c == 'ML' && chatterGroup.name == 'C' ){
c.CollaborationGroupId=chatterGroup.id;
c.MemberId = user.Id;
}
chatGpMem.add(c);
insert chatGpMem;
}
Trigger:
trigger grpUser on User (after insert,after Update)
{
clsName.Groups(trigger.newMap.keySet());
}