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
Jordan GrubbJordan Grubb 

Copy attachments from child to parent

Hi All,

I need help with some code as im new to apex and salesforce.

I need to be able to have it so any file added to the service appointment will copy into the parent record (Work order) 

Any help would be really appreciated. 

Thank you! 
mukesh guptamukesh gupta
Hi Jordan,

This is example code:- 
trigger CopyAttachments on CustomObject(after insert)

{
Attachment[] attList = [select id, name, body from Attachment where ParentId = :Trigger.new[0].Opportunity__c];
Attachment[] insertAttList = new Attachment[]{};
        for(Attachment a: attList)
        {
              Attachment att = new Attachment(name = a.name, body = a.body, parentid = Trigger.new[0].id);
              insertAttList.add(att);
        }
      if(insertAttList.size() > 0)
      {
           insert insertAttList;
      }
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
Jordan GrubbJordan Grubb
I need something more like this to copy all attachments from the service appointments to the work order.
 
trigger CopyAttachments on ServiceAppointment (before insert) {


Attachment[] attList = [select id, name, body from Attachment where ParentId = :Trigger.new[0].WorkOrder__c];
Attachment[] insertAttList = new Attachment[]{};
        for(Attachment a: attList)
        {
              Attachment att = new Attachment(name = a.name, body = a.body, parentid = Trigger.new[0].id);
              insertAttList.add(att);
        }
      if(insertAttList.size() > 0)
      {
           insert insertAttList;
      }
    
}

Thank you!