function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
CodeHeartsSFDCCodeHeartsSFDC 

Error while trying to add contact to Public group

Hi Guys,

I'm trying to add contacts to a public group when a flag on contact 'Add to group' is set to True but receiving Attempt to de-ref null object error on the line highlighted. Any idea what is wrong? Thanks in advance. Pasting the trigger & helper class below.

Trigger:
  1. trigger addToGroup on Contact (after insert, after update) {
  2.  
  3.     List<GroupMember> groupList = new List<GroupMember>();
  4.     
  5.     
  6.     Set<String> contactEmails = new Set<String>();
  7.     
  8.     for(Contact con : Trigger.New) {
  9.     
  10.         contactEmails.add(con.email);
  11.     }
  12.      
  13.      Map<String,User> emailUserMap = new Map<String,User>();
  14.  
  15.      for(User u:[select id, email from User where email in : contactEmails]) {
  16.         emailUserMap.put(u.email, u);
  17.      }
  18.      
  19.      List<Id> userIdList = new List<Id>();
  20.      for(Contact con : Trigger.New) {
  21.         if(con.Add_to_Group__c == True) { 
  22.             userIdList.add(emailUserMap.get(con.email).id);  
  23.         }
  24.      }  
  25.      
  26.      Group grp = [select id from Group where Name = 'Account Group'];
  27.      if(null != grp) {
  28.  
  29.         ContactTriggerHelper.addUsersToGroup(grp.id,userIdList);
  30.     }
  31.  
  32. }
Helper class:
  1. public class ContactTriggerHelper {
  2.  
  3.     @future
  4.  
  5.     public static void addUsersToGroup(String groupId, List<Id> userIds) {
  6.     List<GroupMember> gmList = new List<GroupMember>();
  7.         for(ID userId: userIds){
  8.  
  9.             GroupMember gm = new GroupMember();
  10.             gm.GroupId = groupId;
  11.             gm.UserOrGroupId = userId;
  12.             gmList.add(gm);
  13.         }
  14.        
  15.         if(gmList.size() > 0) {
  16.             insert gmList;
  17.         }
  18.     }
  19. }
Raj VakatiRaj Vakati
You can try like below
 
rigger addToGroup on Contact (after insert, after update) {
 
    List<GroupMember> groupList = new List<GroupMember>();
    
    
    Set<String> contactEmails = new Set<String>();
    
    for(Contact con : Trigger.New) {
    
        contactEmails.add(con.email);
    }
     
     Map<String,User> emailUserMap = new Map<String,User>();
 
     for(User u:[select id, email from User where email in : contactEmails]) {
        emailUserMap.put(u.email, u);
     }
     
     List<Id> userIdList = new List<Id>();
     for(Contact con : Trigger.New) {
        if(con.Add_to_Group__c == True) { 
		if(emailUserMap.get(con.email)!=null){
            userIdList.add(emailUserMap.get(con.email).id); 
		}			
        }
     }  
     
     Group grp = [select id from Group where Name = 'Account Group'];
     if(null != grp) {
 
        ContactTriggerHelper.addUsersToGroup(grp.id,userIdList);
    }
 
}

 
Raj VakatiRaj Vakati
Is its wokring ?
CodeHeartsSFDCCodeHeartsSFDC
@Raj 

I tried the solution which you have provided, but it did not add the contact to the group. I'm not receiving the error now but the contact is still not being added to the public group