2

I am writing a before insert trigger on EmailMessage object to block emails if the fromAddress is in a list of email addresses I have put together. I'm not quite sure on what to set so that the email is never delivered to salesforce. I want to essentially bounce/delete the email so that it doesn't trigger any code. I tried to delete the email by setting email.IsDeleted = true;but that field is not writable. Any ideas?

1
  • not sure about this and need to try this out but did you try throwing a custom exception? Apr 13 '15 at 4:37
1

Assuming you are using On Demand Email to Case, you can configure disallowed email domains/email addresses as shown here in the Email to Case settings under your routing address

enter image description here

Alternatively, you can configure your own mail server to block incoming emails from evildomain.com sent to support@mycompany.com

By the time the trigger on EmailMessage is invoked, the Case SObject is created. I'm not sure if SFDC inserts the Case in the same transaction as it inserts the EmailMessage but if it does (see debug log), you could use addError() to cause the transaction to fail - however, you may see sysad messages indicating transaction failures that you'll have to filter out after-the-fact.

Another option I've seen done on SFSE is an After Insert EmailMessage trigger that deletes the parent Case if the source EmailMessage is from evildomain.com.

But, best to block the message before your application ever sees it.

2
  • I can't use the "Accept Email From" feature because I don't know who it is I'm accepting the email from. I need a way to do this with apex Apr 14 '15 at 21:11
  • 1
    I gave you a couple of suggestions above for apex as well
    – cropredy
    Apr 14 '15 at 22:28

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.