[SalesForce] Sending an email from a trigger with an addError

We have the requirement that if a Case is created via email2case and the Account has a flag (SecureSupport__c) checked then we should block the case creation and send a notification to the contact that created the case saying "you can't do that."

I wrote a trigger that works correctly and does an addError on the record if the SecureSupport__c flag on the account is set. This works great. However any operation try to use to send the email never happens because the entire transaction is rolled back with the addError.

I have tried:

  • Messaging.SingleEmailMessage
  • Inserting a new object that fires a Workflow email
  • Messaging.SingleEmailMessage in an @future call

None of these work. Is there a way that I am missing?

NOTE: I cannot let the case be created and then deleted later. It cannot stay as a record even for a little while

Best Answer

I am pretty sure that the only way that you can do what you are looking for would be to create a 'Email Services' class rather than using the generic email2case feature - this is a class that implements Messaging.InboundEmailHandler and it requires that you do all of the work yourself (such as ding the message, looking up contacts, determining if the message is a reply to an existing email, etc), but because it runs at a 'lower level' than the email2case, there is much more that you can do.

See the following for details:

https://help.salesforce.com/htviewhelpdoc?id=code_email_services.htm&siteLang=en_US

https://help.salesforce.com/apex/HTViewHelpDoc?id=code_email_services_editing.htm&language=en_US

Related Topic