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
DeptonDepton 

Cloning Attachments from one object to another

I am trying to clone attachments from opportunities to a custom object.

Once I create the custom object from the opportunity page I would like to have all the attachments from the opportunity cloned in the new related record.

 

Any idea or sample code for this?

 Thank you!!!

Best Answer chosen by Admin (Salesforce Developers) 
Imran MohammedImran Mohammed

You can start of with the below code. 

The below code is not bulkified, so be careful when using dataloader to insert records in Custom Object.

 

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;

       }

 

}

All Answers

Imran MohammedImran Mohammed

You can start of with the below code. 

The below code is not bulkified, so be careful when using dataloader to insert records in Custom Object.

 

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;

       }

 

}

This was selected as the best answer
DeptonDepton

Hi Imran, this worked like a charm as I told you!

 

Now i would like to deply it to production, any help you can provide with the test method I would really appreciate!

 

Thanks again!

Imran MohammedImran Mohammed

I just saw this thread, are you done with test methods and deployment to production.

DeptonDepton

Hi,

 

Thank you for getting back to me.......unfortunately not i guess

 

1) Create the Parent record

2) Create an attachment to the parent record

3) Create your Milestone1_Project__c record and Insert the record.

 

The insert should fire off your trigger and should copy the attachment from your parent record.

 

4) Query the attachment object to get the attachment from the Milestone1_Project__c.

 

5) Use System.assert to verify

 

but being honest do not know how to do it....if you can help with this please?

 

the deployment is not a problem but the test it is!!!!

 

:)

VennilaVennila

Great. 

Sarada RVS 2Sarada RVS 2
Hi Imran,
I have used above idea which you suggested in one of my requirement , but it doens ent seem to work for me.
Requirement: Move /Insert  any attachments on accounts to creditwriteup(custom object)  after a creditwriteup is created.
Creditwriteup for every account is created using a process builder.
Below is the code iam using.which does not work.
Could you please help.

trigger CopyAttachments on Credit_Writeup__c(before insert,after update)
{
    List<Contact> cList = new List<Contact>();
    List<Lead> lList = new List<Lead>();
    List<Attachment> al = new List<Attachment>();
    List<Attachment> insertAttList = new List<Attachment>();
    Credit_Writeup__c Cw = new Credit_Writeup__c();
    
    
  Id accnt =  String.valueOf(Trigger.new[0].Credit_Writeup_Account__c).substring(0, 15);
  system.debug('account is '+accnt);
    if(accnt != null){
    
    clist = [select id  from contact where accountId= :accnt];
    }
    
    if( cList.size() > 0  && cList != null) {
    
        lList = [SELECT convertedContactId FROM Lead WHERE  convertedContactId IN: cList];
    }
    
    
    if(lList != null&&lList.size()>0){
     
     aL = [select id,body,name from Attachment where parentId= :lList[0].convertedContactId Limit 1] ;
     
    }
    
    
    if(aL != null&&aL.size()>0){
    // public static List<Attachment> insertAttList = new List<Attachment>();
     
        
        for(Credit_Writeup__c c: Trigger.new)
        {
            if(C.CW_AttachmentUpdate__c == True){
            if(Trigger.isInsert){
           
                   Attachment att = new Attachment(name = aL[0].name, body = aL[0].body, parentid = c.id);
                   insertAttList.add(att);
                
            
        }   
             
   
Sourabh KhoslaSourabh Khosla
@Depton @Imran: Were you able to write test class for this piece of code?
Gary Horn 4Gary Horn 4
~Fa`.sOnxGQFu