+ Start a Discussion

Emails being fired twice on trigger

Hello all!


This is my first foray into the world of Apex coding. I'm not very famiiliar with coding so appologies in advance for simple errors!


I have put the below trigger together piecing various bits of code accross various forums together to trigger an email to the creator of a task on completion if that task is assigned to another person. It works great! However, the user gets two emails each time - is there a way around this? I did a search on the forum and coultn't find a previous thread which shared similat code to my own.






Here's the code:


trigger Trigger_Request_CompletedAfterInsertAfterUpdate on Task (after update)
   Task[] completedTasks = Trigger.new;
   for(Task t : completedTasks){
      if(t.OwnerId!=t.CreatedById && t.Status=='Completed'){
         Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();      


    mail.setSubject('Task Completed');        

    mail.setPlainTextBody(Datetime.now().format('MM/dd/yyyy hh:mm a')+' \r\n'+t.Description+' \r\n'+' \r\n'+'Salesforce link: \r\n'+'https://emea.salesforce.com/'+t.AccountId);
    mail.saveAsActivity = false;
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });


Do you have any workflow field updates on task? Because as per the order of execution, all before & after triggers will be executed first, then workflow field udpates will be executed which will again fire the triggers. If this is the scenario, then you can use a static variable in your trigger to send the email only once.


But I would recommend to use workflows to send emails rather than triggers.

Suresh RaghuramSuresh Raghuram

after looking into the code i did not find any mistake


but my doubt is on   for(Task t : completedTasks){


could you check with the completedTasks List is having single task or multiple for a single user.

Thanks for the quick reply! I do have a field update workflow so this must be the problem. Would you mind giving me an example of how I can incorporate a static variable? I'd love to use an email send workflow but it seems that Salesforce won't allow email sending as an action to a workflow in the task object.
Jerun JoseJerun Jose

The idea is to have a static variable which will sustain its value across different executions of the trigger for a single transaction.


Use something like this.

trigger Trigger_Request_CompletedAfterInsertAfterUpdate on Task (after update){
		SomeClass.someValue = false;
		Task[] completedTasks = Trigger.new;
		for(Task t : completedTasks){
			if(t.OwnerId!=t.CreatedById && t.Status=='Completed'){
				Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();      


				mail.setSubject('Task Completed');        

				mail.setPlainTextBody(Datetime.now().format('MM/dd/yyyy hh:mm a')+' \r\n'+t.Description+' \r\n'+' \r\n'+'Salesforce link: \r\n'+'https://emea.salesforce.com/'+t.AccountId);
				mail.saveAsActivity = false;
				Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

 And the class would be


public class SomeClass{
	public static boolean someValue = true;

 Please note that although this is albeit working mechanism, it does have some minor flaws. There was another thread which had detail on using static sets to use a better design, but I cant find it now.