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

  • 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.