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 

Emailing multiple "single emails" - Error if one bounces

In a workflow, I am triggering through apex trigger code that fires off emails.  Even though each email is triggered individually by each workflow (field update on record that in turn fires off apex trigger), salesforce is batching these together, thus the trigger is getting called with multiple updates at once.   No problem.  Code handles that.

 

What it is having issues with is when one of those emails fails due to a bounced email address.  In this case, it seems none of the emails got sent (at least no activities exist on any of the records).

 

Anyone have an idea how to force the other's to be sent and only fail on the actual invalid email record?

 

1.  Since salesforce is combining the field updates into a batch, fix must be batch aware.  So I can't call "SendEmail" once for each email since governor limit restricts at 10 and we have ~100 being sent.

2.  Instead of using Apex, call out to an external webservice and generate emails there?  I would prefer not to go this route (been forced to on few other issues), but it is an option.

3.  ?

 

 

Basic code (simplified for example) is as follows - From a trigger, identify the list of contacts to be emailed and call method passing list of contacts.

 

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

            }

      }

 

Message Edited by Malathan2 on 11-16-2009 03:35 PM
_Andreas__Andreas_

You can specify a Boolean opt_AllOrNone:

 

Messaging.sendEmail(listMessages, False);

 

as documented here: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_email_outbound.htm

 

Regarding for 3?, look at workflows and e-mail alerts: http://login.salesforce.com/help/doc/en/creating_workflow_alerts.htm
This approach would require using an e-mail template, but has an expandable limit of 1000 e-mails/day/user up to an org-wide total of 1,000,000 per day.

David Roberts 4David Roberts 4
document has moved to: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_forcecom_email_outbound.htm#!