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
Malathan2Malathan2 

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); } } }

 

Chirag MehtaChirag Mehta
@Malathan2: By any chance did you found any solution (or alternate approach) as we too are experiencing similar issue ?
Malathan2Malathan2
Too long to remember the workaround to this issue.  The main thing I learned from this issue (and the many many others I have had since then) is most bugs from Salesforce's standpoint seem to be "by design", and I usually spend more time teaching their support staff how to use their product than gaining help (IE, I call it to confirm something works the way I suspect, but will have to spend the time teaching them that this feature actually exists in their product, what menu/location to find it at, and how to use the feature before the support rep can go "find the answer").

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.
.
Kayla Borg 14Kayla Borg 14
Dealing with this issue myself right now. They should have an overloaded method that allows you to specify if you want it to be a bulk insert of activities or not. I believe my work around will be to set (Messaging.SingleEmailMessage) mail.setSaveAsActivity(false) and then to create the activities myself and insert them all together.