You need to sign in to do that
Don't have an account?
SendEmail + Log Activity (Task) + Not Batch Enabled = "Undocumented Feature"?
I have been going around with Salesforce for a while about an "Undocumented Feature". They stand behind the viewpoint that thie issue in question is by design and not a bug. I however feel it is otherwise. Wanted to get other's views on this.
Issue is that if you send an email out, it generates an activity log (task). This is what we want.Salesforce uses batch processing, so whether you insert 1 record or 100 records, the associated trigger should be called only once.Problem is, when sending out 100 emails it generates 100 activity logs, but each is created individually, thus the trigger is called 100 times.
If the Task trigger were to perform any action, such as reading from the database, you just hit your limit due to the email api not working in batch mode when creating activity logs! If everything is Salesforce is setup to work in batches, why not the SendEmail function, which takes a list of email objects to send in batch?
Code to reproduce this is as follows (look at log file to see Task Trigger is being called for every email sent).
trigger SalesforceEmailIssue_Task on Task (before insert) { System.debug('********** SalesforceEmailIssue_Task *****************'); } public class SalesforceEmailIssue { public static void GenerateEmails(List<Contact> listContacts){ System.debug('********** SalesforceEmailIssue *****************'); System.debug('# of contacts: ' + listContacts.size()); List<Messaging.SingleEmailMessage> listMessages = new List<Messaging.SingleEmailMessage>(); // Build list of messages for(Contact c : listContacts){ Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); mail.setSaveAsActivity (true); mail.setHtmlBody('Test'); mail.setSubject('Test'); mail.setTargetObjectId (c.Id); listMessages.add(mail); } // Send emails try{ Messaging.sendEmail(listMessages); } catch(Exception ex){ System.debug('Exception occured: ' + ex); System.Assert(false, ex); } } //######################################## // TEST //######################################## public static testMethod void test(){ integer numOfEmails = 10; string emailAddress = 'claytond@axiumae.com'; try{ System.debug('********* CREATE ACCOUNTS ******************'); List<Account> listAccounts = new List<Account>(); for(Integer i = 0; i < numOfEmails; i++) listAccounts.add(new Account(name='Test' + i, BillingState = 'OR', Type='Prospect')); insert listAccounts; System.debug('********* CREATE CONTACTS FOR EACH ACCOUNT ******************'); List<Contact> listContacts = new List<Contact>(); for(Account a : listAccounts){ //Contact c = AxTestRecords.createContact(a.Id, true); listContacts.add(new Contact( FirstName = 'John' , LastName = 'Doe' , AccountId = a.Id , MailingState = 'OR' , Contact_Role__c = 'Billing Contact' , Email = emailAddress) ); } insert listContacts; GenerateEmails(listContacts); } catch(Exception ex){ System.debug('Exception occured: ' + ex); System.assert(false, ex); } } }
But to the actual issue above, I believe the main controlling factor we have in place is we restrict the size of the batch to be processed (think around 100 or 150). Any more and we sometimes start hitting limits and issues.
.