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

  • Tricky one. I don't know if in a Trigger you can do this the way you want. Are you required to only use Email2Case? If you use Email2Apex to create the Case you could try/catch the exception successfully and send the email since you can write your own Apex Class to handle the inbound email. Granted you lose some of the nice features of Email2Case but maybe you could do something like forward the Inbound Email from your ApexEmailHandler to your Email2Case? Getting creative here....... – CoryCowgill Aug 28 '14 at 22:11
  • came across this question salesforce.stackexchange.com/questions/9502/… – apn Aug 28 '14 at 22:25
  • @CoryCowgill yes, we are already using Email2Case and this is just an extension of that. – Patrick Connelly Aug 29 '14 at 12:33
  • if you used Email2Case Agent rather than Email2case On demand, then you could modify the SFDC-supplied Java code to handle the Case insertion error and send an email back to the sender – cropredy Aug 29 '14 at 23:22

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:



  • Yeah, I was afraid that this was going to be the solution. Oh well, we'll just live without being able to reply back because adding a new Messaging.InboundEmailHandler is way out of scope for this. – Patrick Connelly Sep 12 '14 at 14:18

In case, if someone facing an issue now(v47.0) We do have an alternate solution.

Please have those addError method in try-catch-finally block. Finally has higher preferences so it would execute anyway. Now you can publish a Platform event. Now write a platform event trigger which will have the code to send the email.

Following is the dummy code working:


trigger AccountTrigger on Account (before insert) {
    catch(Exception e){
        EventBus.publish(new SendEmail__e () ); //there is a platform event created named as - SendEmail         

SendEmail - Platform Event trigger

trigger sendEmail on SendEmail__e (after insert) {
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        string[] to = new string[] {'trysocialme@gmail.com'};
        string[] cc = new string[] {'trysocialme@gmail.com'};


        email.setSubject('Test Acct Mail ');

        email.setHtmlBody('Hello World');
        Messaging.SendEmailResult[] results = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });
        System.debug('TestEmail0' + results);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.