4

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());
}
4
  • Can you debug your collection before your insert and ensure your id types match the expected type? Commented Aug 26, 2016 at 19:53
  • @Anusha Can you make your class without sharing ? Commented Aug 26, 2016 at 20:10
  • @JesseMilburn I debugged and I got the expected values
    – Anusha
    Commented Aug 26, 2016 at 20:18
  • @MohithShrivastava I added without Sharing but still it is showing same error
    – Anusha
    Commented Aug 26, 2016 at 20:25

1 Answer 1

0

Can you make this changes as follows:

Where matching group members to be added inside if block and chatGpMem list to be inserted outside for loop:

@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;
            chatGpMem.add(c);
        }
        else if(user.reg__c == 'DL' && chatterGroup.name == 'B' ){
            c.CollaborationGroupId=chatterGroup.id;
            c.MemberId = user.Id;
            chatGpMem.add(c);
        }
        else if(user.reg__c == 'ML' && chatterGroup.name == 'C' ){
            c.CollaborationGroupId=chatterGroup.id;
            c.MemberId = user.Id;
            chatGpMem.add(c);
        }
}
if(chatGpMem.size()>0) insert chatGpMem;
}
2
  • Thanks for your response, It is working ! But it is invoking 2 times
    – Anusha
    Commented Aug 26, 2016 at 20:45
  • you can use static variable to restrict second time call. Commented Aug 26, 2016 at 20:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .