function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Trigger Email alert when a new attachment is added to an Opportunity



Colleagues are required to add attachments to Opportunities that they do not own, however there is no possible way using standard salesforce functionality to send an email alert to the Opportunity owner.


Does anyone know how I can achieve this using triggers?


Your help would be much appreciated! Thanks...



trigger Attachment_Processor on Attachment (after insert) {

map<id, opportunity> opps = new map<id, opportunity>();
Opportunity o = null;

for(attachment a:[select parentid from attachment where parent.type = 'Opportunity' and id in :trigger.keyset()]){

o = new Opportunity(Id = a.parentid, review_needed__c = true);
opps.put(a.parentid, o);


update opps.values();


 then make a workflow to look for changes to the review needed field. In the workflow rule, send the email and set the review needed flag back to false.



Thanks for your response ehartye.


I am not very familiar with triggers etc and I am receiving the below Compile Error message:



Error: Compile Error: Method does not exist or incorrect signature: trigger.keyset() at line 6 column 96


Any ideas? Thanks in advance...


aaah my bad :)


you want to use: trigger.NewMap.keySet()




Thanks ehartye. I have used the below Apex Trigger and this is active, but does not seem to be firing and updating the review_needed field?



trigger Attachment_Processor on Opportunity (after insert) {

map<id, opportunity> opps = new map<id, opportunity>();
Opportunity o = null;

for(attachment a:[select parentid from attachment where parent.type = 'Opportunity' and id in :trigger.NewMap.keySet()]){

o = new Opportunity(Id = a.parentid, review_needed__c = true);
opps.put(a.parentid, o);


update opps.values();


Anyone got any ideas? Thanks...


You should be running the trigger on Attachment, not opportunity. Sorry for the delayed response.


Unfortunately I get the below error message:




   Error: Compile Error: Incorrect SObject type: Attachment should be Opportunity at line 1 column 1




You can't just rename the trigger and save. You have to delete it and create a whole new trigger.


I have created a new trigger but still receive the error message..


Error: Compile Error: Incorrect SObject type: Attachment should be Opportunity at line 1 column 1


The only reason you would get that error is if you were trying to change the SObject target of an existing trigger. You may want to try renaming it.


I have tried this from fresh in another sandbox that we have, however I still receive the same error message??


Did you get a reply to this or figure it out?  I'm having the same problem.  The error seems to indicate that the trigger is not operating on Attachments, but there doesn't seem to be an interface to add a new "Attachment Trigger" in Salesforce because there is no menu for "Attachments" under the App Setup > Customize area of the console.

Michael BobeeMichael Bobee
I got the following error:  Error: Apex trigger Attachment_Processor caused an unexpected exception, contact your administrator: Attachment_Processor: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.Attachment_Processor: line 6, column .
 I want to set a flag on the opportunity when an attachment is added so I can add a workflow action. But this fail occurs before I attept to save it.
Raghu NaniRaghu Nani
hi acnet,
Even i got also same error but i fixed,
I think u r Creating trigger on opportunity object ,u need to create trigger on attachment,but it is hidden object,
so u need to write a trigger on Developer Console,In this way i fixed My problem,May This helps to u,,
Code Shows Below:

trigger restrictDelete on Attachment (before delete) {
    for(attachment at:trigger.old){
        at.adderror('Sorry you can\'t delete this attachment u don\'t have permission' );