Im followed the advice I found on Bob Buzzard's blog to try and automatically add users to chatter groups. http://bobbuzzard.blogspot.com/2011/11/automatically-add-user-to-chatter-group.html

But for some reason my version of the setup isnt working?

User Trigger:

trigger addToGroup on User (after insert) {

Utility Class:

public class UserMethods {

    public static void addToMainGroup(Set<Id> userIds) {
     List<User> users=[select id, Username from User where id in :userIds];

     CollaborationGroup chatterGroup=[select Id from CollaborationGroup Where Name='Change Machine Community'];
     List<CollaborationGroupMember> chatterGroupMembers=new List<CollaborationGroupMember>();

     for(User user: users){
         CollaborationGroupMember newMember = new CollaborationGroupMember(CollaborationGroupId=chatterGroup.id, MemberId = user.Id, NotificationFrequency = 'P');

     insert chatterGroupMembers;



If i run the code in addToMainGroup by itself (with a hard coded userId) in the console it does work properly, but when i register a new user it never seems to run? No errors but it doesnt add the record either?

Any ideas?

UPDATE: The seems to be getting the function to run for self register users... because users added by a sysadmin are working fine but anyone whose self registered doesnt.... and suggestions on how to work around this

  • Can you add some debug statements to see where you go wrong. chattergroup.size() and maybe chatterGroupMembers.size() – Chris Duncombe Nov 4 '13 at 15:27
  • Having a hard time figuring out how get these to show in debug logs, added some debugs and ran the trigger but nothing showing.. Related to that i did run the code as anonoymous apex with just a hardcoded userId and it worked fine... so the group lookup and member record saving seem to be working fine? – Keith Mancuso Nov 4 '13 at 16:23
  • Is the group in a community? – greenstork Nov 4 '13 at 16:26
  • Yes, its in communities, and the user in question is a communities member – Keith Mancuso Nov 4 '13 at 17:57
  • So the issue seems to be one of permissions. I have this as part of a self register process but because its triggered by the public site user its not running.... is there anyway to have a trigger run as someone else? – Keith Mancuso Nov 5 '13 at 14:58

I found an answer to the issue...

The issue was the trigger was being triggered by the "public portal user" so what i did was make an empty page that the user was redirected to after being registered like this

Id userId = Site.createPortalUser(u, accountId, password);

return Site.login(u.Email, password, '/joinGroup');

then join group is an empty page which just added them to the group (using the method above), and then redirects to the group page.

Thanks everyone for all your help


<apex:page controller="joinGroupController" action="{!addToGroup}">


public PageReference addToGroup() {
  Id userId = UserInfo.getUserId();
  Set<Id> userIds = new Set<Id>();
  CollaborationGroup chatterGroup=[select Id from CollaborationGroup Where Name='Change Machine Community'];
  string groupURL = '/_ui/core/chatter/groups/GroupProfilePage?g='+chatterGroup.Id;
  return groupURL;
| improve this answer | |

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.