+ Start a Discussion
Varun ChaddhaVarun Chaddha 

Task Email Notifications via APEX not working, could be a Bug in platform?

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 enabeld for user in My Setitngs:

Email notifications enabled for user setting

Global Settings for Allow User to control Notifications also Enabled:

User-added image


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 ahve triggerred 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 "Enable User Control Over Task Assignment Notifications" globally for all users.

 
Varun ChaddhaVarun Chaddha
It's been very long and no one has responded, but to me it seems a global issue with everyone getting effected. I really look forward to have someone from Salesforce taking a note of this and confirm if there is an ongoing Issue for this in there system or not.


To further narrowdown the issue, I was able to confirm that, this issue is there when we create Tasks from Batch APEX, and not from normal usual APEX controller class.

To confirm this you can follow following Code Classes/VF Pages to see that issue exist in platform regarding this:

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){/**/}

}

Now, we can see that, 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 cofnirms this theory, by logic, the notifications should work correctly even from Batch Apex, as it is APEX Api.

 
Roger WickiRoger Wicki
Im also looking forward to an official statement. Good work on pointing this out / proving it.

I would have a different use than you, but it most likely roots in the same issue.
Varun ChaddhaVarun Chaddha
Also, the online documentation here does not differentiate between Batch Apex or normal apex when it stated that Task Notifications in Apex using triggerUserEmail DMLOption will respect Global Task Notification settings:
http://docs.releasenotes.salesforce.com/nl-nl/winter15/release-notes/rn_sales_activities_notifications_task_assignment.htm
Varun ChaddhaVarun Chaddha
@Roger - Do you know how can we get these issues/bugs to Salesforce? I tried opening Support cases but support usually clsoe them abruptly stating Development Support is not provided, and thus it does not even get attended to see if it actually is a bug or a functionality.
Roger WickiRoger Wicki
@Varun Chaddha - Unfortunately not. If there's not a Salesforce Partner (& Developer) raising a case, we have little to no chance that it'll get through...
Varun ChaddhaVarun Chaddha
Latest update is:

It definitely was a bug in platform, but no one recognized it. I could very well reproduce it via 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. So I'm glad and thus thought of updatign this post here that the issue seems to be resolved in Pre-release org, and I hope it remains fixed with final Summer '15 platform release.
Roger WickiRoger Wicki
Thanks for the Update. Unfortunately, this fix does not help with my issue :D But at least a further bug is resolved.

Best
Roger