3

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...

1 Answer 1

4

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

4
  • Thanks for clarifying that for me. However, any way to suppress while in a before insert or before update trigger context? Would I need to launch an @future to update these? Commented Sep 4, 2014 at 18:15
  • 1
    Ahhhh yes, I missed that part of your question where it stated these were before triggers. Yes, a future method is the way to go. I have updated the answer and added a knowledge article. Commented Sep 4, 2014 at 18:19
  • Thank you, sir. I'll implement that shortly and let you know how it goes... Commented Sep 4, 2014 at 18:20
  • Oh man, I am now running into an issue where that @future is failing unit tests because i have a batch job that updates lead records (Future method cannot be called from a future or batch method). Any ideas? Commented Sep 4, 2014 at 21:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .