Skip to main content
Notice removed Improve details by Deployment Failure
Bounty Ended with Phil Hawthorn's answer chosen by Deployment Failure
edited title
Link
Deployment Failure
  • 4.9k
  • 16
  • 89
  • 190

Copying attachments from object to object trigger

edited title
Source Link
Deployment Failure
  • 4.9k
  • 16
  • 89
  • 190

I have a custom object called Server_Order__c used in my customer portal which is then mirrored to mythe Opportunities object, as well as mirroring the data I would also like to also copy overmirror any 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 objectthat are uploaded.

Edit: Due to the way SF works, attachment are uploaded to their own object and justhave reference the parentA 'Service Order' object which means the record is not updated. What I would like to know is how can I have any attachments uploaded to my Service Ordera child of Opportunities object, they should be also copied to the relevant Opportunity. The mirrored opportunity (ID) is referenced in my Service Order aseach record contains a Opportunity__c foriegn key.

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

trigger CopyAttachmentstoOpps on Attachment (after insert) {

    List<Attachment> insertAttList = new List<Attachment>();
    
    for (Attachment att:Trigger.new) {           
        String parentIdString = String.valueof(att.parentId);
        Service_Order__c oppId = [SELECT Opportunity__c FROM Service_Order__c WHERE ID = :att.parentId];
        system.debug('The ID is' + oppId);
        
        Attachment b = att.clone();
        b.parentid = parentIdString;
        insertAttList.add(b);                 
    }            
}

EDIT
My initial code has a trigger on Service Order updates, however the way Salesforce works when an attachment is uploaded it is stored in a child object hence no updates are made to the parent. I have modified my code accordingly but I am still unable to get it to work.

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, attachment are uploaded to their own object and justhave reference the parent object which means the record is not updated. What I would like to know is how can I have any attachments uploaded to my Service Order object, 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 CopyAttachmentstoOpps on Attachment (after insert) {

    List<Attachment> insertAttList = new List<Attachment>();
    
    for (Attachment att:Trigger.new) {           
        String parentIdString = String.valueof(att.parentId);
        Service_Order__c oppId = [SELECT Opportunity__c FROM Service_Order__c WHERE ID = :att.parentId];
        system.debug('The ID is' + oppId);
        
        Attachment b = att.clone();
        b.parentid = parentIdString;
        insertAttList.add(b);                 
    }            
}

I have a custom object called Server_Order__c used in my customer portal which is mirrored to the Opportunities object, as well as mirroring the data I would also like to mirror any attachments that are uploaded.

A 'Service Order' object is a child of Opportunities object, each record contains a Opportunity__c foriegn key.

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

trigger CopyAttachmentstoOpps on Attachment (after insert) {

    List<Attachment> insertAttList = new List<Attachment>();
    
    for (Attachment att:Trigger.new) {           
        String parentIdString = String.valueof(att.parentId);
        Service_Order__c oppId = [SELECT Opportunity__c FROM Service_Order__c WHERE ID = :att.parentId];
        system.debug('The ID is' + oppId);
        
        Attachment b = att.clone();
        b.parentid = parentIdString;
        insertAttList.add(b);                 
    }            
}

EDIT
My initial code has a trigger on Service Order updates, however the way Salesforce works when an attachment is uploaded it is stored in a child object hence no updates are made to the parent. I have modified my code accordingly but I am still unable to get it to work.

edited title
Link
Deployment Failure
  • 4.9k
  • 16
  • 89
  • 190

Copying attachments from custom object to Opportunitiesobject

Notice added Improve details by Deployment Failure
Bounty Started worth 50 reputation by Deployment Failure
deleted 87 characters in body
Source Link
Deployment Failure
  • 4.9k
  • 16
  • 89
  • 190
Loading
added 20 characters in body
Source Link
Deployment Failure
  • 4.9k
  • 16
  • 89
  • 190
Loading
added 416 characters in body
Source Link
Deployment Failure
  • 4.9k
  • 16
  • 89
  • 190
Loading
Source Link
Deployment Failure
  • 4.9k
  • 16
  • 89
  • 190
Loading