Activity › Forums › Salesforce® Discussions › How to write the Salesforce Trigger for a Custom Object’s Attachment?
Tagged: Attachments, Attachments in Salesforce, Custom Object Permissions, Salesforce Custom Object, Salesforce Custom Objects, Salesforce Trigger, Salesforce Trigger Condition
-
How to write the Salesforce Trigger for a Custom Object’s Attachment?
Posted by Archit on March 23, 2018 at 11:36 AMI have to write a Salesforce trigger for custom Object‘s Attachments.
please suggest the exact path… where I can write a trigger for attachment.
Manpreet replied 8 years, 1 month ago 3 Members · 4 Replies -
4 Replies
- [adinserter block='9']
-
Hello Archit,
We cannot write the Triggers on Attachment object.
Create and schedule a batch process which will create the attachments in Attachments object.
Thanks,
-
Hi Archit,
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;
}
}
Hope this helps you.
Please let me know if this works out.
-
Hello Pranav,
I think we should also add an event of after update as well in trigger because sometimes attachments can also be changed as per requirements. I think we should also focus on same.
Thanks,
Log In to reply.