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 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.
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];
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.
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.
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
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
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.
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.
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;
}
}
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.
Hi Pradeep,
You have really solved this issuse... i am really thankful to....
Hi Pradeep,
You have really solved this issuse... i am really thankful to you....
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
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