Database.DMLOptions dm = new Database.DMLOptions();
dm.EmailHeader.triggerUserEmail = true;
Task t = new Task(Subject='Test Task', ActivityDate=date.today(), OwnerId='00590000000wND2');
database.insert(new Task[]{t}, dm);
Email Notifications are enabled for user in My Settings:
Global Settings for Allow User to control Notifications also Enabled:
Based on these settings the Email Notification should have received by the User. And the OwnerId
passed in the code is NOT the Logged In User
, so the code should have triggered email notifications. Please correct me if I'm wrong, but it looks like something is broken here. The code sends out Email Notifications ONLY when I've disabled
the Enable User Control Over Task Assignment Notifications
globally for all users.
After doing more debugging I found that normal APEX does work fine but the issue exist in Batch Apex.
Try following Visualforce Page/Classes. The Notifications work in case of button calling controller class function but does not work when another button calls the same function using Batch Apex.
Visualforce Page Code:
<apex:page controller="NotificationsTest">
<apex:form>
<apex:commandButton action="{!createTask}" value="Create New Task" />
<apex:commandButton action="{!createTaskBatch}" value="Create New Task (batch)" />
</apex:form>
</apex:page>
APEX Controller Class:
public with sharing class NotificationsTest {
public NotificationsTest() {
}
public void createTask() {
Database.DMLOptions dm = new Database.DMLOptions();
dm.EmailHeader.triggerUserEmail = true;
Task t = new Task(Subject='Test Task', ActivityDate=date.today(), OwnerId='00590000003uTpH');
database.insert(new Task[]{t}, dm);
}
public void createTaskBatch() {
NotificationsTestBatch taskBatch = new NotificationsTestBatch();
Database.ExecuteBatch( taskBatch );
}
}
Batch APEX Class:
global with sharing class NotificationsTestBatch implements Database.Batchable<SObject>, Database.Stateful{
/**
* Constructor
*/
public NotificationsTestBatch() {
}
/**
* Use the start method to collect the records or objects to be passed to the interface method execute
*/
global Iterable<sObject> start( Database.BatchableContext bc ){
return [select Id, Name from Account];
}
/**
*
* @param bc
* @param data
*/
global void execute( Database.BatchableContext bc, List<SObject> data ){
try{
NotificationsTest n = new NotificationsTest();
n.createTask();
}catch(Exception e){
AsyncApexJob a = [Select Id, Status, NumberOfErrors, JobItemsProcessed,
TotalJobItems, CreatedBy.Email,ExtendedStatus
from AsyncApexJob where Id =
:bc.getJobId()];
// If there were any errors Send an email to the Apex job's submitter
// notifying of job completion
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {a.CreatedBy.Email};
mail.setToAddresses(toAddresses);
mail.setSubject('EXCEPTION during Batch creation ' + a.Status);
mail.setPlainTextBody ('The batch Apex job processed ' + a.TotalJobItems + ' batches.\n\nError :'+e+'\n\nError Detail: '+e.getStackTraceString()+'\nLine Number: '+e.getLineNumber()+'\nType: '+e.getTypeName());
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
/**
*
*/
global void finish(Database.BatchableContext BC){/**/}
}
When I click "Create Task" button, it initiates a normal APEX class function, and in that way I receive Task Assignment Notification.
But when I click "Create Task (batch)" button, it initiates a Batch apex call to the function and that does not send any Task Assignment Notification. I don't see anywhere in the docs that confirms this theory, by logic, the notifications should work correctly even from Batch Apex, as it is APEX Api.
Based on this documentation:
EDIT - 3-May-2015 : It definitely was a bug in platform, but no one recognized it. I could very well reproduce it following code posted in here, but a quick test in Summer '15 Pre-release organization revealed that it has been fixed. I could not reproduce it Summer 15 Org but could reproduce that Batch Apex generated Tasks were not sending Task Notifications.