Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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.

share|improve this question
    
Turns out, you literally can just write that. But not through Salesforce interface. Please see the accepted answer below. –  Simon Lawrence Feb 13 at 12:14
add comment

1 Answer 1

up vote 4 down vote accepted

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

Developer Console Eclipse

See here for more information:
http://help.sfdcdro.com/apex/HTViewSolution?id=000181538&language=en_US

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

URL

share|improve this answer
    
..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 at 12:06
    
Have updated a trick to even work through SFDC UI Interface –  Mohith Shrivastava Feb 13 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 at 12:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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