Skip to main content
2 of 7
added 416 characters in body
Deployment Failure
  • 4.9k
  • 16
  • 89
  • 190

Copying attachments from custom object to Opportunities

I have a custom object called Server_Order__c used in my customer portal which is then mirrored to my Opportunities, I would like to also copy over attachments to the relavent Opportunity.

How the attachment can be linked to the right Opportunity is by getting the Opportunity__c value from Service_Orders__c object.

Edit: Due to the way SF works, when uploading attachments once a record has been made this doesnt count as an update to the record itself and hence my code will not work as desired. What I would like to know is how can I have any attachments uploaded to a Service Order, they should be also copied to the relevant Opportunity. The mirrored opportunity (ID) is referenced in my Service Order as Opportunity__c.

Here is my apex trigger as of now, but it does not seem to work.

trigger CopyAttachments on Service_Order__c (after update) {

 Attachment[] attList = [select id, name, body from Attachment where ParentId = :Trigger.new[0].name];
 Attachment[] insertAttList = new Attachment[]{};

         for(Attachment a: attList)
         {
               Attachment att = new Attachment(name = a.name, body = a.body, parentid = Trigger.new[0].Opportunity__c);
               insertAttList.add(att);
         }

       if(insertAttList.size() > 0)
       {
            insert insertAttList;
       }
Deployment Failure
  • 4.9k
  • 16
  • 89
  • 190