+ Start a Discussion
Joe GaskaJoe Gaska 

Exception/Email Question and/or Observation


Good Morning;


To make a long story shot I want to capture an exception, send email, then re-throw the exception.  It seems that if any exception gets thrown it does a full rollback including the email messages.  The integration system(s) need the exception thrown for tracking purposes so it has to be thrown regardless.  Is there any way to send an email and have it sent even if an exception is thrown?  For background purposes below is the scenerio


1.) SalesForce.com webservice is called from an integration layer.

2.) The webservice updates data in SalesForce.com 

3.) Webservice is wrapped with try catch

4.) Within catch we send an email to SFDC administrators and rethrow the error for integration layer.


We can obviously send the email within the integration layer but would love to keep all SFDC business logic within SalesForce.com.  If I remove the throw the email(s) get sent no problem but we need to throw the exception.


I need to get around "The email is not sent until the Apex transaction is committed."






catch(Exception ex)
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();                                       
            String EmailBody =  '<HTML>\n<BODY>\n<TABLE BORDER="1">' + 
                                '\n<TR><TH ALIGN="LEFT">Date\\Time of Transaction</TH><TD ALIGN="LEFT">' + String.ValueOf(DateTime.now()) +  '</TD></TR>' +                                                     
                                '\n<TR><TH ALIGN="LEFT">Error Message</TH><TD ALIGN="LEFT">' + ex +  '</TD></TR>' + 
                                '\n<TR><TH ALIGN="LEFT" colspan="2"><HR/></TH></TR>' +      
                                '\n<TR><TH ALIGN="LEFT">Invoice Message</TH><TD ALIGN="LEFT"><PRE>' + invoice +  '</PRE></TD></TR>' +                             
            String[] toAddresses = new String[] {'SalesforceAdmins@abiomed.com', 'Support@IoniaSolutions.com'};                                                                                         
            mail.setSubject('[Invoice Processing Failed]');                                                       
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
            throw(ex)I nee