function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
surya prasad G Asurya prasad G A 

Copying Attachments from parent object to child object when field changes

Hi,

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?

Thanks,
Prasad.
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);
               insertAttList.add(att);
            }   
         }
       if(insertAttList.size() > 0)
       {
            insert insertAttList;
       }
 
}