You need to sign in to do that
Don't have an account?

How can I add a contact to a public group with Apex?
I am using the below code
trigger AddToPG on Contact (after insert, after update) {
List<GroupMember> GMlist = new List<GroupMember>();
for(Contact U : Trigger.New) {
if(U.Add_to_Group__c == TRUE) {
GroupMember GM = new GroupMember();
GM.GroupId = '00Gg0000000afKH';
GM.UserOrGroupId = U.Id;
GMList.add(GM);
}
}
insert GMList;
}
However, this is giving the below error
Error:Apex trigger AddToPG caused an unexpected exception, contact your administrator: AddToPG: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, User/Group ID: id value of incorrect type: 003g000000HfUsiAAF: [UserOrGroupId]: Trigger.AddToPG: line 12, column 1
I think this is because I am trying the set the contact ID where I should be using the User or Group ID. The contacts will be Partner users so they will have a user record. Are there any changes I can make to my trigger which will allow me to do this?
trigger AddToPG on Contact (after insert, after update) {
List<GroupMember> GMlist = new List<GroupMember>();
for(Contact U : Trigger.New) {
if(U.Add_to_Group__c == TRUE) {
GroupMember GM = new GroupMember();
GM.GroupId = '00Gg0000000afKH';
GM.UserOrGroupId = U.Id;
GMList.add(GM);
}
}
insert GMList;
}
However, this is giving the below error
Error:Apex trigger AddToPG caused an unexpected exception, contact your administrator: AddToPG: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, User/Group ID: id value of incorrect type: 003g000000HfUsiAAF: [UserOrGroupId]: Trigger.AddToPG: line 12, column 1
I think this is because I am trying the set the contact ID where I should be using the User or Group ID. The contacts will be Partner users so they will have a user record. Are there any changes I can make to my trigger which will allow me to do this?
A work around is to use a future method which calls the code to add the user to the group.
the trigger:
NOTE: I've made the way you get the group id dynamic, so please place 'test grp' with your group name.
the helper class:
All Answers
you can refer below code :
If this solves your problem, kindly mark it as the best answer.
Thanks,
Vatsal
i have been able to save the code without error and I can also save the contact record without error but the contact is not added to the group. Any idea why?
What you need to do is search for the corresponding user the contact relates to, once you have found the user, you can add that user to the group.
How do you know what user is related to a contact?
That is what I am trying to do. The users I want to add to the groups will be partner users, so they will have a partner user record. the problem is, I don't know how to get the contacts related partner user ID. Any idea how I can get this?
If the user is created before the contact, you can create a new custom field (relatedUser__c) on the contact object which looks up to the related user. If you can do this, then your code would look something like:
Also, ideally you should have the trigger on after insert only because currently the trigger will be called when ever the user is updated.
Thanks but it is still not adding the User/Contact to the group. The record and trigger save without error but the Contact /User is not added to the Group. I am using the group ID from the URL on the Group detail page. Is this the correct ID? If so, is there any other reason why the user is not added to the group?
A work around is to use a future method which calls the code to add the user to the group.
the trigger:
NOTE: I've made the way you get the group id dynamic, so please place 'test grp' with your group name.
the helper class:
Thank you so much. It is working perfect. 1 more question. At the moment the trigger is firring based on - "if(con.Add_to_Group__c == TRUE)" . I was thinking of having multiple checkboxes like this one. The idea is that depending on which checkbox is True, the contact(user) is added to the relevant group.
Example -
If checkbox 1 is true = Add user to group 1
If checkbox 2 is true = Add user to group 2 etc....
Where in the code would I add the additional fields and Groups?
You need to add the further IF statments like in line 19. You will also need to move all the logic to the future method as with the future annotation cannot take sObjects or objects as arguments.
So basically you pass a list of IDs to the future method and that does all the work.
Hope that helps.