Activity Forums Salesforce® Discussions How to write the Salesforce Trigger for a Custom Object's Attachment?

  • Archit

    Member
    March 23, 2018 at 11:37 am

    Actually, I am not able to find out whether I can write trigger on attachment or not. Please help me out.

    • This reply was modified 6 years, 9 months ago by  Archit.
  • Manpreet

    Member
    March 23, 2018 at 11:40 am

    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,

  • PRANAV

    Member
    March 23, 2018 at 11:40 am

    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.

  • Manpreet

    Member
    March 23, 2018 at 11:45 am

    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.

Popular Salesforce Blogs