+ Start a Discussion
surya prasad G Asurya prasad G A 

Copying Attachments from parent object to child object when field changes


I have requirement as below:
- To copy the Attachments from Opportunity object record to child object record  when date field changes in Opportunity object record.
Can anyone please share me the code?

Gaurav Jain 7Gaurav Jain 7
Hi Surya,

Please try below code:

Mark it as Best Answer, if it helps​​
trigger CopyAttachments on Opportunity (after update)
 Attachment[] attList = [select id, name, body from Attachment where ParentId = :Trigger.new[0].Id];
 Attachment[] insertAttList = new Attachment[]{};
 Expense__c[] opplinelist = [select id,name from Expense__c where Opportunity__c = :Trigger.new[0].Id];
         for(Attachment a: attList)
             for(Expense__c tmpobj : opplinelist){
               Attachment att = new Attachment(name = a.name, body = a.body, parentid = tmpobj.id);
       if(insertAttList.size() > 0)
            insert insertAttList;