Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

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

share|improve this answer
    
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? –  Nathan Williams Sep 4 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. –  sfdc_ninja Sep 4 at 18:19
    
Thank you, sir. I'll implement that shortly and let you know how it goes... –  Nathan Williams Sep 4 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? –  Nathan Williams Sep 4 at 21:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.