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
PandeiswariPandeiswari 

Send Email is not creating bulk tasks

Hi,

I have functionality to send 100 emails when Click on Notify button.

        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
            for(Contact con : contact)
            {
                Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
                email.setTargetObjectId(con.Id); 
                email.setWhatId(accountId); 
                email.setSaveAsActivity(true);
                mails.add(email);
            }
            if(mails.size()>0)
            {
                Messaging.SendEmailResult [] r = Messaging.sendEmail(mails);
            }    
            
    When emails are sent, it will create Task record in contact. I have trigger on Task Object to update field in account. When 100 task records are getting created, it is calling SOQL query in my below trigger 100 times. Please advice if my trigger is bulkified or is there any issue in Email sending code.
    
    List<account> accountId  = new List<ACcount();
    List<Task> triggerTask = Trigger.new != null ? Trigger.new : Trigger.old;
            for (Task task: triggerTask){
               String accountId = task.WhatId;
               if(task.Status =='Completed' && task.Isclosed==true && task.IsRecurrence == false && accountId !=null){
                   accountId.add(task.whatId);
               }
            } 
      
        
       if(accountId.size()>0){  
           List<accountId> accountList = [SELECT Id, (SELECT Id FROM Tasks where Status='Completed' and Isclosed=true and IsRecurrence = false) FROM Account WHERE Id IN :accountId];
          
           for(Account acc:accountList) {
               if(acc.Tasks != null) {
                     acc.Activities__c= acc.Tasks.size();
                   }
              }
            if(accountList.size()>0){
               update accountList;
            }
        }    
SonamSonam (Salesforce Developers) 
Where is the below code located?
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
            for(Contact con : contact)
            {
                Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
                email.setTargetObjectId(con.Id); 
                email.setWhatId(accountId); 
                email.setSaveAsActivity(true);
                mails.add(email);
            }
            if(mails.size()>0)
            {
                Messaging.SendEmailResult [] r = Messaging.sendEmail(mails);
            }    

Is it in a trigger as this doesn't seem to be bulkified...
PandeiswariPandeiswari
Hi Sonam. Thanks for your response. Above code to send mail is in Controller (Not in trigger). Controller will get called when we select mutiple contact and click on "Notify" button in visual force page.
Dennis AtwoodDennis Atwood
Did you ever get an answer to this?  I'm working on the same problem.  It appears that Messaging.sendEmail() causes the Task trigger to fire individually for each email sent.  I can't tell if that is because of something in our org or if that's just expected behavior.
Dennis AtwoodDennis Atwood
I've tested this in my own developer org and found that Messaging.sendEmail does in fact fire EmailMessage and Task triggers individually for each email in the list.  That's unfortunate.
rollorollo
Hi,

any news on this issue. Seems like a bug.