+ Start a Discussion

Show stopper for go Live - system.Exception: Too many Email Invocations: 11

As per our client requirement, I have written a trigger on Task. Details are below:
Business Case:
Customer will perform some business functionality at Offline and will create at least 60 and more Tasks at offline and will assign to different Contacts. After Synchronization, system will insert all these Tasks at Online. For every task we need to send a separate mail which will have text + some HTML tags (TextBox, check box, submit button etc.. ) to concern Contacts. But due to Salesforce Limitations, I'm getting the following error:
18:26:59.732|EXCEPTION_THROWN|[68,53]|System.Exception: Too many Email Invocations: 11

We would like to have your immediate attention for this issue, as this is the top most show stopper for Go-Live and the Go-Live date is already delay.

Source code:
trigger TaskMail on Task (after insert,after update){
Set<Id> taskIds=new Set<Id>();
for(Task t:Trigger.New){
} // end of trigger

public class TaskFuture{

public static void sendMassMails(Set<Id> taskIds){
Map<Id,Task> taskMap=new Map<Id,Task>([select Id,whoID,Send_Mail_to_Contacts__c,Subject,Account__c,Product__c,Status,Priority,Description from Task where Id IN :taskIds]);
Set<Id> contactIds=new Set<Id>();
for(Id key:taskIds){
Map<Id,Contact> contactMap=new Map<Id,Contact>([select Id, Name, Email from Contact where ID IN :contactIds]);
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
//for mass email
//List<Messaging.MassEmailMessage> mails =new List<Messaging.MassEmailMessage>();

//Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
//EmailTemplate e1 = [select Id,Name,Subject,body from EmailTemplate where name like :'TaskTemplate'+'%'];
String url ='http://salesforce.drreddys.in:8080/DRLLive/DRLTaskSubmit';
// DRL-Test Server
// String url ='http://salesforce.drreddys.in:8080/DRLTest/DRLTaskSubmit';
//Bodhtree Public IP server
// String url= '';
for(Id key:taskIds){
Task t=taskMap.get(key);
mail = new Messaging.SingleEmailMessage();
//mail =new Messaging.MassEmailMessage();
Contact c=contactMap.get(t.whoID);
// ===============For MOM=======================
if(t.WhoID!=null && t.Send_Mail_to_Contacts__c==true){

String[] toaddress = new String[]{c.Email};
// mail.setTargetObjectIds(new Id[] {c.Id});
//User ur = [select Name from User where ID=:t.OwnerId];

// mail.setTemplateId(e1.Id);
//mail.setWhatIds(new Id[]{t.Id});
mail.setHtmlBody('<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>' +
'<title>DRL Task Update</title></head>'+
'<body><form action="'+url+'" method="post" target="_blank"><p>Hi <B>'+c.Name+'</B><br/>A New Task has been created for <br/> Account : '+t.Account__c+'<br/>Product :'+t.Product__c+'. Details of the Task are:<br/><br/> <B>Subject :</B>'+' '+t.Subject+
'<br/><table border="1"><tr><td>Status : </td><td>'+t.Status+'</td></tr><tr><td>Contact : </td><td>'+c.Name+'</td></tr><tr><td>Priority : </td><td>'+t.Priority+'</td></tr><tr><td>Comments : </td><td>'+t.Description+'</td></tr></table>'+

'<br/><b></p>To Update The Values ,Please Do it in the Following Fields : </b><table border="1">'+
'<input type="hidden" name="taskId" value="' + t.Id + '"/>'+
'<br/>Status :&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <select name="Status"><option value="Not Started">Not Started</option><option value="In progress">In Progress</option><option value="Completed">Completed</option><option value="Delayed">Delayed</option> </select>'+
'<br />Description : <input type="text" name="Description" /><br />' +
'<br/><input type="submit" value="Update" /> </table>'+
'<p><br/>Thanks<br/> Salesforce Support. </p></form></body></html> ');

}catch(Exception e){
System.debug(' Exception in Email Try Block .....\t'+e);
} // end of for
List<Messaging.Email> allMails = new List<Messaging.Email>();
for( Integer j = 0; j < mails.size(); j++ ){
Messaging.SendEmailResult[] results = Messaging.sendEmail( allMails );
} // end of method
} // end of class


I haven't analyzed your code thoroughly, but have you considered using workflow rules with email alerts? What is the reason for using triggers/Apex instead?