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
Casey SchelerCasey Scheler 

Copy/Move attachments from one object to another (not related)

I need to move/copy an attachment located on the opportunity object to a custom object. The objects are not related. We need the move/copy done soon after (automatically) the customer service team uploads the attachment to the opportunity. We have a custom object created for our production team and they do not have visibility to the opportunity. Any suggestions? We currently use Informatica but they don't have the ability to move attachments yet. I saw ideas of using the data export but we need it to be automated. The custom object is automatically created at the time the opportunity is changed to closed/won status. Any suggestions? Can I trigger be created for this?
Steven NsubugaSteven Nsubuga
Yes, a trigger can definitely get the job done. However, check if it is not possible to Visual Workflow in stead. I am 100% certain a Trigger can do the job.
Casey SchelerCasey Scheler
What's the advantage of Visual Workflow vs. Trigger? I will look into the Visual Workflow but how would I go about getting the task done using the trigger?
 
Steven NsubugaSteven Nsubuga
Visual Workflow vs. Trigger? 
Admins can easily learn and use Visual Workflow. Triggers require code writing skills. 

The custom object is automatically created at the time the opportunity is changed to closed/won status.
How is this being done?
 
Casey SchelerCasey Scheler
It is being created with the use of informatica integration but they don't have the ability to move attachments. 
Steven NsubugaSteven Nsubuga
There needs to be some kind of relationship between the Opportunity and the custom object, maybe in the way the custom object is named, or ideally a Lookup on the custom object to the Opportunity. The Trigger we write would then be able to find the relevant custom object and copy the attachment over to this custom object.
Casey SchelerCasey Scheler
The opportunity refers to a job number, in our case 5 digits. The custom object also named by the job number. Would this help?
 
Steven NsubugaSteven Nsubuga
If the custom object had a Look up field to Opportunity called OppId then the following trigger would get the job done.
trigger copyAttachment on Attachment (after insert) {

    List<Attachment> attachmentList  = new List<Attachment>();
    Map<Id, Attachment> oppAttachmentMap = new Map<Id, Attachment>();

    for(Attachment att: Trigger.new){
        oppAttachmentMap.put(att.ParentId, att);
    }

    List<CustomObject__c> objList  = [select Id, OppId  from CustomObject__c where OppId IN:oppAttachmentMap.keyset()];

    for(CustomObject__c obj: objList){
        Attachment att = oppAttachmentMap.get(obj.OppId);
        att.ParentId = obj.id;
        attachmentList.add(att);
    }

    update attachmentList;
 
}