3

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());
}
  • Can you debug your collection before your insert and ensure your id types match the expected type? – Jesse Milburn Aug 26 '16 at 19:53
  • @Anusha Can you make your class without sharing ? – Mohith Shrivastava Aug 26 '16 at 20:10
  • @JesseMilburn I debugged and I got the expected values – Anusha Aug 26 '16 at 20:18
  • @MohithShrivastava I added without Sharing but still it is showing same error – Anusha Aug 26 '16 at 20:25
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;
}
  • Thanks for your response, It is working ! But it is invoking 2 times – Anusha Aug 26 '16 at 20:45
  • you can use static variable to restrict second time call. – Santanu Boral Aug 26 '16 at 20:55

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.