function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Abhi_TripathiAbhi_Tripathi 

Send Email Once When Creating new Task

Hi,

 

I have writtern a code on my task Trigger to set "Send Email Notification" to true, but using that its sending email to assigned user Two times,

 

Really not getting the logic and how to solve it ?

 

My code is

 

//List of task
List<Task> taskClone = [Select Id From Task Where Id=:tasks.keySet()];
	
		//Set EmailHeader.triggerUserEmail to true
		Database.DMLOptions dmlo = new Database.DMLOptions();
		dmlo.EmailHeader.triggerUserEmail = true;
		
		System.debug('@@@@@@' + taskClone);
			
		//Update Task 	
		database.update(taskClone, dmlo);  

 

 

Any help would be appreciated

Sonam_SFDCSonam_SFDC

Hi,

 

Did you try setting up debug logs for this user triggering the creation of task?

if not please do - the logs will give a better idea as to the flow.

 

I understand that this list of task should return only on task right?

List<Task> taskClone = [Select Id From Task Where Id=:tasks.keySet()];
Abhi_TripathiAbhi_Tripathi

No, this trigger is bulk, and that  map.keySet() will have all the Ids of tasks Inserted.

Sonam_SFDCSonam_SFDC

Was researching a bit on this and found the following article:

https://help.salesforce.com/apex/HTViewSolution?urlname=Why-does-DMLOptions-EmailHeader-triggerUserEmail-not-prevent-the-email-notification-sent-when-a-case-or-lead-owner-is-changed&language=en_US

 

What I undertand is that when a new task is create - the mail will automatically be sent so you do not need to explicitly update the checkbox - your code does that which might be the reason its sending two emails..

 

Pls comment the code and see how it works

Abhi_TripathiAbhi_Tripathi

Thanks for the reply sonam,

 

 User need to check that "Send Email Notification" manually to send email, but here am making that checkbox to true using the above code, and when i comment it out, it sends nothing, so there is something happennning which i cant see

 

You can run that code in After Insert event with Trigger.newMap, and then insert a task and check the email of Assigned user

GlynAGlynA

Abhi,

 

Are there any Workflow rules on the Task object in your org?  Do they do field updates?  If so, the trigger might be running twice and sending an email both times.  You can use a static Boolean variable in a helper class to prevent the trigger from doing anything on the 2nd execution.

 

-Glyn

Abhi_TripathiAbhi_Tripathi

Thanks for the reply @Glyn,

 

No there is nothing, which is sending email, the problem is 

 

When I use @future method, the same code sends email once, but when I remove futurde anotation its sending twice.

 

Can you tell me why is this thing happening and How can I modify it.

GlynAGlynA

Abhi,

 

I think I have an answer.  When the code runs in the trigger, the Task record is in a pending DML.  You set the email option to true and update the Task, while it is in a pending DML.  Your update completes and sends an email, and then the pending DML completes and sends another email, because the email option is still true.

 

If the code is in an @future, the pending DML completes before the @future runs, and so the email option is still false.  Then, the @future sets the email option to true and does an update, which sends an email when it completes.

 

If I'm right, then you should be able to prevent the second email by clearing the email option before the pending DML completes:

//List of task
List<Task> taskClone = [Select Id From Task Where Id=:tasks.keySet()];
	
//Set EmailHeader.triggerUserEmail to true
Database.DMLOptions dmlo = new Database.DMLOptions();
dmlo.EmailHeader.triggerUserEmail = true;
		
System.debug('@@@@@@' + taskClone);
			
//Update Task 	
database.update(taskClone, dmlo);

dmlo.EmailHeader.triggerUserEmail = false;

This might be completely wrong.  Let me know what happens.

 

-Glyn

 

 

 

Abhi_TripathiAbhi_Tripathi

Hey Glyn,

 

Thanks for the reply I have tried that you suggested, but nope Its not working correclty, stuck in same situation of recieving two emails of task.

ThisIsRSNThisIsRSN
Hi Glyn, Abhi,

Glyn was right, this issue was because of pending DML operation. I tried to fix this using a future method and it worked.
You need to send the Email for the task outside the scope of pending DML, so that it will not have any duplicate effect. Similarly, if you use these dml options in before triggers, nothing will happen as mentioned by sonam post.
I used this code:
trigger testTaskEmail on Task (after Insert) {
    list<id> tidList = new list<id>();
    for(task rec: trigger.new){
        tidList.add(rec.id);
    }   
    testFuture.testTask(tidList);         
}
& class:
public class testFuture{
   @future
   public static void testTask(list<id> tIds){
       List<Task> taskClone = [Select Id From Task Where Id=:tIds];
       //Set EmailHeader.triggerUserEmail to true
        Database.DMLOptions dmlo = new Database.DMLOptions();
        dmlo.EmailHeader.triggerUserEmail = true;       
        System.debug('@@@@@@' + taskClone);           
        //Update Task  
        database.update(taskClone, dmlo);
   }
}

Thanks
RSN