I have a scenario in which I would like to fire off some Apex functionality when an Attachment is inserted from an EmailMessage object (that is received via Email-to-case).

The trouble is, it does not appear that you can have triggers on standard child relations (such as ContactRole, Attachment) - and if you try to access these children using Triggers on the parent, the children "do not exist yet" because they cannot be inserted until this parent has been and an ID is available.

To this end, we have tried writing an After Insert Trigger on EmailMessage and fire it on an incoming e-mail we know to have attachments, but (whilst the "Has Attachments" flag is set) querying for the respective Attachments returns null. We have also tried using workflow, and flag fields to try to find a point in time (in this single processing request) that the attachments have come into existence, but alas we have had no luck.

Our ultimate goal is actually to re-parent all the attachments coming in on an E-mail, away from the EmailMessage and put them on a custom object (which already exists). We want to stay away from using time-delay/time-based solutions (such as a scheduled job that runs every hour to sweep up e-mails + attachments) as this is part of a responsive support-desk solution, and also there is a lot of other involved functionality. I really, really just want to be able to write:

trigger AttachmentTrigger on Attachment (before insert) {

Any input or experience would be most welcome.

  • Turns out, you literally can just write that. But not through Salesforce interface. Please see the accepted answer below. – Simon Lawrence Feb 13 '14 at 12:14

You can write trigger on attachment from the Developer console or the Eclipse IDE.

Developer Console Eclipse

See here for more information:

If you want to get this on UI try the below hack:

locate entity query string parameter in the URL when creating a new trigger on any object (e.g. entity=Case) and change it to Attachment (entity=Attachment) and press Enter


  • ..ok! Thank you, we're just trying this out, with horrified faces that the SFDC Interface provides no such facility for this. – Simon Lawrence Feb 13 '14 at 12:06
  • Have updated a trick to even work through SFDC UI Interface – Mohith Shrivastava Feb 13 '14 at 12:07
  • 2
    Thank you very much. We are pretty stunned here. In the end we discovered from your link we could do this through the developer console. I think I'm going to go work on a Java project for a bit.. – Simon Lawrence Feb 13 '14 at 12:12

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.