You need to sign in to do that
Don't have an account?
CodeHeartsSFDC
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:
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:
- trigger 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) {
- 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);
- }
- }
- public class ContactTriggerHelper {
- @future
- public static void addUsersToGroup(String groupId, List<Id> userIds) {
- List<GroupMember> gmList = new List<GroupMember>();
- for(ID userId: userIds){
- GroupMember gm = new GroupMember();
- gm.GroupId = groupId;
- gm.UserOrGroupId = userId;
- gmList.add(gm);
- }
- if(gmList.size() > 0) {
- insert gmList;
- }
- }
- }
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