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?


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:

enter image description here

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()];
update u;

the checkbox is cleared:

enter image description here

  • 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. – Naveen Jan 20 '15 at 10:31
  • It looks like the private message email notifications are controlled by the UserPreferencesDisableMessageEmail property on the User sObject. – alouie Jan 21 '15 at 6:55
  • I have set 'UserPreferencesDisableMessageEmail' property to true. Even now the private message email notifications are getting triggered. – Naveen Jan 21 '15 at 9:02

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;

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.