I am trying to stop the default email notifications for a particular User in salesforce through apex code. Stopping default email notifications can be done manually by going to My Settings -> Email Settings and unchecking 'Receive emails' checkbox. I need to achieve the same thing through Apex code. Please suggest how can this be done?
3 Answers
Have you tried the settings on the NetworkMember object? For Communities, this seems to take precedence. For a similar issue, I poked it via the developer console with this query:
SELECT Id, PreferencesDisableAllFeedsEmail FROM NetworkMember WHERE MemberId = ''
Assuming you are referring to the chatter email settings feature:
You can clear this checkbox by setting the UserPreferencesDisableAllFeedsEmail field to true for the user. In the screenshot above my Receive emails checkbox is set, but if I execute the following Apex code:
User u=[select id, UserPreferencesDisableAllFeedsEmail from User
where id=:UserInfo.getUserId()];
u.UserPreferencesDisableAllFeedsEmail=true;
update u;
the checkbox is cleared:
-
Thanks for your reply. But I am trying to disable all the email notifications to a Community User. Not only chatter email notifications, but i need to disable notifications for Private messages too. I have tried the above code which you have given but still it is triggering Private message notification email.– NaveenJan 20, 2015 at 10:31
-
It looks like the private message email notifications are controlled by the UserPreferencesDisableMessageEmail property on the User sObject.– alouieJan 21, 2015 at 6:55
-
I have set 'UserPreferencesDisableMessageEmail' property to true. Even now the private message email notifications are getting triggered.– NaveenJan 21, 2015 at 9:02
-
Wonder where we can find the full list of which API name corresponds to what.. eg. UserPreferencesDisableAllFeedsEmail to toggle the "Receive emails" .. how about <apiname> to toggle the "Mentions me in a post"– compskiApr 20, 2021 at 9:36
Run below code in developer console to disable chatter only for community users.
Profile p = [Select Id,name from Profile where name = 'System Administrator'];
List<NetworkMember> nmList = [Select Id,MemberId,NetworkId,PreferencesDisableAllFeedsEmail FROM NetworkMember WHERE Member.ProfileId != p.Id];
for(NetworkMember nm: netMemList){
nm.PreferencesDisableAllFeedsEmail = true;
}
update nmList;