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
SMasterSMaster 

Where to write the Trigger for Attachment

Hi,

 

I have to write a trigger for custom Object's Attachments..

 

please suggest the exact path.. where i can write a trigger for attachment...

Best Answer chosen by Admin (Salesforce Developers) 
Pradeep_NavatarPradeep_Navatar

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

skodisanaskodisana

Hi,

 

We cannot write the Triggers on Attachment object.

Create and schedule a batch process which will create the attachments in Attachments object.

 

Thanks,

Kodisana

Imran MohammedImran Mohammed

We can write triggers on Attachment object.

If you are using Force.com IDE in Eclipse, you can right click on the triggers folder and select create new trigger.

In that you will find the Attachment object.

If you don't see Attachment object, then check the checkbox shown below the list of objects.

 

Hope this helps.

ngabraningabrani

If you are using native Salesforce user interface, then you can go to Create-->Objects, and select your object. Go to the Triggers section, and click on New to start a new trigger on this object.

Pradeep_NavatarPradeep_Navatar

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
Cory CowgillCory Cowgill

Imram and Praddep are correct, however I would be careful with this approach.

 

Just last week I created a trigger on the Attachment object on before insert, before update. The trigger appears to work in Unit Tests and anywhere you call 'insert attachment' explicitly.

 

However, in the general Salesforce UI, default pages which do Attachment inserts do not always fire the before insert trigger on the Attachment object.

 

I escalated this issue to Salesforce premier support, and they escalated it to Tier 3 (R&D/Engineering) who responded that it is a known bug in Salesforce and they are working on resolving it, but they did not provide an ETA.

 

So if this works for you great. If you see weird behavior and don't see your trigger firing, just know your not crazy it may be the existing Salesforce bug.

SMasterSMaster

Hi Pradeep,

You have really solved this issuse... i am really thankful to....

 

 

SMasterSMaster

Hi Pradeep,

You have really solved this issuse... i am really thankful to you....

 

 

Divya_RemedyforceDivya_Remedyforce

will this code work?

I think co.size will give size of custom object list..

How to get the size of attachments in a custom object

RamanaRamana
You can write trigger on Attachment using Force.com Developer Console as well.  Also, you can enable Notes and Attachment on Custom Object only when creating teh Custom Object.
Beer NutthawanBeer Nutthawan
Using Developer Console:
 User-added image

User-added image
 
Bruno GrilliBruno Grilli
Hi.

Still no fix on it? Just began with SF and I realized that after insert triggers on Attachment linked to an Event does not work????

Am I right?

Thank you!
Bruno