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
Ilene JonesIlene Jones 

How to prevent Chatter Group Auto-Follow from Generating Email - Database.DMLOptions

Currently I have a trigger which auto-follows specific groups in our organization when a new user is brought onboard.  The auto-follow trigger feature works great!  However, we have quite a few groups we would like these users to follow, based on various departments, which means that the user will get emails for each of the groups they are added to.  I would like to turn off email notifications during this process.

To do this, I found Database.DMLOptions.triggerUserEmail.  This seems to be exactly what I am looking for, however the sending of th email persists.  Below is the short function which is called by the trigger.  As you can see, I tried adding the options to both the cand ChatterGroupMember object and to the database.insert.  Using only one of these email header option sections also continues to send the email - as if it is being completely ignored.

Any assistance would be greatly appreciated.

public static void AddToGroups(Set<Id> userIds)
    {
        List<User> users = [select Id, Username from User where Id in :userIds];             
        Set<Id> updateUsers = new Set<Id>();
        // set up the groups that the user should be added to
        /*  All Users should be changed to the correct name of the group containing the department list */
        List<CollaborationGroup> chatterGroups=[select id, Name from CollaborationGroup where name like :ANNOUNCEMENT_GROUPS];

        // Start a new list that we can add new members to for the single update call.
        List<CollaborationGroupMember> chatterGroupMembers=new List<CollaborationGroupMember>();
              
        // loop the users - do not recheck for the flag, it may have already been updated!
        for (User user : users) {
            // loop the groups
            for (CollaborationGroup chatterGroup : chatterGroups) {
                // if is not already member, add the user to the group
                if(IsMember(chatterGroup.id, user.Id) == false) {

                    CollaborationGroupMember cand =
                        new CollaborationGroupMember(CollaborationGroupId=chatterGroup.id, MemberId = user.Id);

                    // Do not send an email to the users about the groups they're joining.
                    Database.DMLOptions dlo = new Database.DMLOptions();
                    dlo.EmailHeader.triggerAutoResponseEmail = false;
                    dlo.EmailHeader.triggerUserEmail = false;
                   
                    // Set the email options on the user we're working with
                    cand.setOptions(dlo);

                    // Now add the user to the group list
                    chatterGroupMembers.add(cand);

                    // why did I do this?
                    updateUsers.add(user.Id);
                }
            }
        }
        // Only run the insert if needed
        System.Debug(chatterGroupMembers);

        // Do not send an email to the users about the groups they're joining.
        Database.DMLOptions dlo = new Database.DMLOptions();
        dlo.EmailHeader.triggerAutoResponseEmail = false;
        dlo.EmailHeader.triggerUserEmail = false;
       
        Database.insert( chatterGroupMembers, dlo);    
    }


Pat PattersonPat Patterson
The only way to do this is to disable all user's chatter emails via the UserPreferencesDisableAllFeedsEmail column, do your CollaborationGroupMember inserts, then revert the users original preferences. See this article for details: How to: Silently add users to Chatter Groups (https://help.salesforce.com/apex/HTViewSolution?id=000176780&language=en_US)
Pat PattersonPat Patterson
Ilene JonesIlene Jones
While this seems to work in the sandbox, this solution does not work in production.  The solution is also specifically geared to be a multi-step process using data loader, while our implementation is through a trigger, using a checkbox on the user object to determine if they should receive this treatment when the user is updated or inserted.   

Other ideas are welcome!