+ Start a Discussion
loveLearningloveLearning 

Trigger On attachment

I need to write some validatio/trigger on attachment object. Is there any possibility of doing that ?

Best Answer chosen by Admin (Salesforce Developers) 
ngabraningabrani

To write a trigger on Attachment you have to use Eclipse. Open your Org in Eclipse then create a new trigger where you will get a picklist to select the Attachment Object. This is the only the way to write a trigger on Attachment. Below is the sample code of the trigger :

 

            // Trigger to update the Attachment__c custom checkbox field in Custom Object(Custom_Obj__c) if it has any attachment.

            trigger TiggerName on attachment (after insert)

            {

                        List<Custom_Obj__c> co = [select id, Attachment__c from Custom_Obj__c where id =: Trigger.New[0].ParentId];

                        if(co.size()>0)

                        {

                                    co[0].Attachment__c = true;

                                    update co;

                        }                                                                   

            }

All Answers

ngabraningabrani

To write a trigger on Attachment you have to use Eclipse. Open your Org in Eclipse then create a new trigger where you will get a picklist to select the Attachment Object. This is the only the way to write a trigger on Attachment. Below is the sample code of the trigger :

 

            // Trigger to update the Attachment__c custom checkbox field in Custom Object(Custom_Obj__c) if it has any attachment.

            trigger TiggerName on attachment (after insert)

            {

                        List<Custom_Obj__c> co = [select id, Attachment__c from Custom_Obj__c where id =: Trigger.New[0].ParentId];

                        if(co.size()>0)

                        {

                                    co[0].Attachment__c = true;

                                    update co;

                        }                                                                   

            }

This was selected as the best answer
JuulJuul

Hi Ngabrani,

 

Thanks for your trigger. How can I add an extra criterium to this trigger. I need to check if an attachment contains the name "RFP".

 

thanks

 

 

ngabraningabrani

You could try something like --

trigger CheckAttachmentName on Attachment (before insert) {

    for (Attachment att:Trigger.new) {
        if(att.name.contains(''RFP) {

        }
     }

}


There is an example at the following link --
http://shivasoft.in/blog/salesforce/creating-trigger-on-attachment-in-salesforce/