[SalesForce] How to stop the “Lead: [full name] has been assigned to you” emails

I'm assigning lead ownership via apex triggers (before insert, before update) and would like to not have it send out the "Lead: [full name] has been assigned to you" email to the new owner. How do I stop those from going out?

Side note: I'm assigning via apex trigger because it's dependent on some other territory assignments that happen based on address during the before insert and before update triggers already, so I'm not interested in Lead Assignment Rules as a solution…

Best Answer

You want to use the DmlOptions.EmailHeader Class for this. The class has 3 properties that can send or suppress system emails. The property you are looking for is triggerUserEmail. Here is an example of how you would use it

 Database.DMLOptions options = new Database.DMLOptions();
 //THE PROPERTY YOU ARE LOOKING FOR
 options.EmailHeader.triggerUserEmail = false; 

 //ADDITIONAL PROPERTIES THAT MAY COME IN HANDY
 options.EmailHeader.triggerAutoResponseEmail = false;
 options.EmailHeader.triggerOtherEmail = false;

 database.update(Record_or_List of records, options);

Here is a link to the documentation

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_class_Database_EmailHeader.htm

EDIT

If you are working with beforte triggers, this will not work. From the docs

it only applies to DML calls made after the DML options are applied. I.e. you would need to generate a list of objects to insert/update and set DMLOptions on them.

Here is a knowledge article that gives the workaround of using future methods to make this work

https://help.salesforce.com/apex/HTViewSolution?id=000176854&language=en_US

Related Topic