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
HarmpieHarmpie 

Trigger on Attachment (before insert) impossible?

Hi. I tried to 'disable' the default Attachment function of SFDC with the following trigger:
 
Code:
trigger AttachmentDisabler on Attachment (before insert) {
 List<Attachment> atts = Trigger.new;
 for(Attachment a:atts) {
  a.addError('Please use the "Attach(FTP)" button on top of the detail page to attach a file.'); 
 }
}

 
But this does not seem to work. Did I do something wrong or is it impossible this way? Even the debug did not show anything, the trigger does not seem to do anything. Any other ways to disable the normal Attach function of SFDC without removing the entire related list? (want to keep notes function)
JeremyKraybillJeremyKraybill
Hi there -- I couldn't find confirmation of this in the docs, but have seen several posts about this that lead me to believe that, indeed, insert triggers on Attachment do not fire. Would love to hear a response from an official Salesforce.com person on that.

The only alternative I can think of is to disable the Notes & Attachments section of your page layouts - if you want to disable the "Attach File" button, do you need that section at all? If you are still working with Note and/or Attachment objects, you probably could write a VF component that would allow users to do what they need to do with Notes and/or Attachments, but not allow them to use the "Attach File" button.
HarmpieHarmpie
Thanks for the response. I am also hoping for some 'official' comment of the SF guys around the boards....
 
As for the work arounds: of course I can easily remove the entire list and simulate notes in a custom object...still would like this question answered though as it has impact on the (im)possibilities of SF, which are important to know for a consultant..
TaoDuhTaoDuh
I am finding the same thing.  Create a trigger on insert, update, and delete of Attachment.  Nothing happens during insert but I get the expected results for the other two operations.  Same trigger.
JakesterJakester

Very interesting. I'm also trying to understand this. I found this post on the IdeaExchange:

http://ideas.salesforce.com/article/show/10093842

 

 which seems to indicate that triggers on attachments is possible (the end of werewolf's response is ... "You can do it for attachments using an Apex trigger.")

 

TaoDuh - are you saying that updates and deletes work on Attachment, but insert doesn't?

Message Edited by Jakester on 05-08-2009 09:14 AM
TaoDuhTaoDuh

That is exactly what I am saying. I don't know if the commenter you reference did something different and was more successful than I.

 

I had a single trigger that both performed updates and logged to the debug console.  The trigger was on update, delete, and insert.  On insert none of the functionality or logging happened where it did on the other two operations.

jduhlsjduhls

So...anyone gotten a fix for this yet?  I'm trying to do the same thing and nothing is happening.

 

 

trigger NewAttachmentAlert on Attachment (before insert) { if(trigger.new.size() < 21) { for (Attachment theAttachment:trigger.new) { Case[] theCase = [select TriggerNewAttachEmail__c from Case where Id = :theAttachment.ParentId]; if(theCase.size() > 0){ theCase[0].TriggerNewAttachEmail__c = true; update theCase[0]; } } } }

 


 

ron_reedron_reed
I just did a test and it appears to be working now.  Can someone else verify they are getting the same results?
MyGodItsColdMyGodItsCold

I tried it & it worked for me. I'm waiting to hear from SF to know it's something we can count on.

 

Currently, we simulate a cron activity queueing up time based workflows that change a field that launches a trigger that looks to see if there's a new attachment & if so, does the work and changes a field which queues up a re-run. However, it only runs 4 of these an hour ...

BeeddiskShahBeeddiskShah
Where is the Attachment trigger written?
MyGodItsColdMyGodItsCold

It's just an APEX Trigger, no different from the rest:

 

trigger CL_Attachment_Processor on Attachment (before insert) { // code goes here ... }

 

 It works like a champ.
DcoderDcoder

Hi All,

 

I am really trying to find where to write trigger on attachment object?

 

regards

dcoder

LoxMyth BourneLoxMyth Bourne

You must use the IDE (Eclipse). I don't see how you can do it using just the salesforce screens.

sohitbhardwajsohitbhardwaj

Trigger has to be (After insert)

 

Here is the sample.

 

trigger AttachmentDisabler on FeedItem  (after insert)

{ List<FeedItem> atts = Trigger.new; 

for(FeedItem a:atts)

{ system.debug(a.contentsize + '@@@'); 

if(a.contentsize > 0)

{  a.body.addError('Please attach file greater then 10'); 

  } 

}

}

 

This trigger restrict uploading of files to chatter.

 

 

Regards,

Sohit Bhardwaj | Developer Support Engineer - Tier 2 Salesforce.com

MyGodItsColdMyGodItsCold

You write the trigger as you would any triggger, by naming the object upon which the trigger acts, Attachment, and specifying the event you want to kick it off. Then, what you need do is figure out to what object the Attachment is being attached. You use the PARENTID for this, and the 1st 3 characters.

 

Once you know you're on the right object being attached to, you may then look for other circumstances in which you want to do what needs to be done. Testing values of the attachment name, testing values of the specific object to which it's being attached, would be 2 examples of knowing your circumstances.

 

Does this answer your question?

toddnewmantoddnewman

I believe Salesforce fixed this problem back in 2009.  I just added a before insert and after insert trigger on Attachment to verify and they both fired.

Lee_CampbellLee_Campbell

I know this topic is ages old now, but would anyone know how to provide a test for the Trigger described in this thread?

chvl narayanachvl narayana
Hi All
i have created two objects.
objA and objB
ObjA is parent and objB is child
when ever any new parent record is created automatically child records are also created. 
now based on child records i want to attch a file with child records data to parent record

plz share your knowledge

thanks 
narayana