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

Problem with dmlo.EmailHeader.triggerUserEmail = true


I wrote a trigger on Lead to create a task for the Lead owner and notify him about the task creation. Here is the code,

List<task> lNewTasks =new List<task>();
Database.DMLOptions dmlo = new Database.DMLOptions();
dmlo.EmailHeader.triggerUserEmail = true;
Task tsk = new Task(Subject = 'Follow Up Lead test',Ownerid = Lead.Owner.ID ,whoid =Lead.ID,, Type='Phone' );
database.insert(tsk, dmlo);

But facing following issues:
1) Sometimes email not sent out. I'm just not getting what is the logic for sending/not sending the email notification on task creation. An email should go out each time task is created with this logic.
2) In email body Lead Name is null. See:
New Task

To: Peter Carmasino

Shruti Gujarathi has assigned you the following new task:

Subject: Follow Up Lead
Lead: null
Due Date: 10/21/2014
Priority: Normal

For more details, click the following link:<Task.ID>

Can anyone help me into this? Why Lead is null in the email sent after task is created? We can not handle this email, so I don't know what else do I need to do in apex to have the Lead name in email?

Thanks in advance!!!
SonamSonam (Salesforce Developers) 
Is this a before or an after insert/update trigger on lead? if its a before, please change it to after and see if that makes a difference.

It is after update trigger.
SonamSonam (Salesforce Developers) 
I'm trying to reprocude the same on my test ORG: Could you share the trigger code here? 
Also, are tasks begin created correctly on the Lead? 
Here is my code:

trigger NotifyLeadOwnerOnAssignment on Lead (after update)
      list<Task> lNewTasks = new list<Task>();     
      Database.DMLOptions dmlo = new Database.DMLOptions();
      dmlo.EmailHeader.triggerUserEmail = true;      
              for(integer i=0; i<; i++)
                 system.debug('old owner@@@'+trigger.old[i].Ownerid);
                 system.debug('new owner@@@'[i].Ownerid);
                 if(trigger.old[i].Ownerid <>[i].Ownerid )
                     Task tsk = new Task(Subject = 'Follow Up Lead',Ownerid =[i].OwnerId ,whoid[i].Id,, Type='Phone' );
                   database.insert(lNewTasks, dmlo);

    catch(Exception e)
SonamSonam (Salesforce Developers) 
Hey, I am able to reproduce this issue on my test ORG, escalating this internally..will keep you updated on this thread..
Hii Sonam..... Did you able to get the solution for this issue?? I really need this sorted .. Plzzz help me
SonamSonam (Salesforce Developers) 
Not yet Shruti, could you please send in your ORG ID and grant me login access to your Admin Account so I can create a case and escalate this email address is sraju @ salesforce dot com
Sent you an email. But I have a concern that this issue is occuring everywhere. And not specific to our org. So not sure escalating a case will help? Or is there a change in salesforce api?
SonamSonam (Salesforce Developers) 
Yes, I understand your concern, therefore, I will be escalating this to Tier3 Support to identify if this is a bug and see if we can find a workaround.
SonamSonam (Salesforce Developers) 
Found a quick workaround to resolve this issue:
Go to : 
Setup > click Customize > Activities > Activity Settings>
Disable "Enable user control over task assignment notifications" at the org level

The team will also be updating you via the case of the same - putting it here to help others who might be facing a similar  issue..

We also had similar issue in production. so we tried to implement the workaround suggested by you. It works in DEV and ST enviroment whereas in UAT enviroment it throws the error "You can’t edit the Activity Settings page now because there’s a Shared Activities update in progress. Try again later."
Karthik jKarthik j
Great, Solution posted long back and still works after all this years.