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
Swaggy BSwaggy B 

Trigger to copy attachments when you clone opportunity

Trying to figure out somehelp on how I can copy the attachments when I clone a opportunity from the original opp. I created a field called Clone_Helper__c that has a workflow rule which evalutes true if it is blank and does a field up date to populate that field with the Orginal opp id.
right now I just have some test stuff in there to see if it would just make a new attachment. 

 if(trigger.isBefore){
        Set<Id> OppsID = new Set<Id>();
        for(Opportunity cloneOpp: Trigger.new){
            if(cloneOpp.Clone_Helper__c != null){
               OppsId.add(cloneOpp.id);
            }
        }
        if(!OppsID.isEmpty()){
            Map<id,Opportunity> oppMap = new Map<id,Opportunity>([Select Clone_Helper__C From Opportunity Where Id IN :OppsId]);
           
            List<Attachment> attachments = new List<Attachment>();
            Attachment thisAttch = new Attachment();
            thisAttch.Name = 'Unit Test Attachment';
            Blob bodyBlob = Blob.valueOf('Unit Test Attachment Body');
            thisAttch.body = bodyBlob;
            thisAttch.parentId = ??
            thisAttch.Description = 'ABC';
            insert thisAttch;
         
           }
      
        }
Shrikant BagalShrikant Bagal
You can refer following 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;

       }

 

}