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
Debendra Ray 8Debendra Ray 8 

Attachments are not copied from Contact to Opportunity after Conversion from Lead - Urgent

I'm trying to copy the attachments from Contact to the Opportunity after Conversion from a Lead - Below is the code, which works only if I manually update the new Account converted from Lead  :

trigger Copy_Attachments_Accts_to_Oppr on Account (after insert,after update) {

    //if (trigger.isAfter && trigger.isInsert) {
        List<Attachment> attachmentsToInsert = new List<Attachment>();
        //List<Attachment> attachmentsToDelete = new List<Attachment>();
        for (Account acc : Trigger.New) { 
               System.debug('DEBUG: Copy_Attachments_Accts_to_Oppr:: Account Id->'+;
               System.debug('DEBUG: Copy_Attachments_Accts_to_Oppr:: Account Parent Lead Id->'+ acc.Account_ID__c);      
               List<Opportunity> opprs = [select Id from Opportunity where Account.Id = ]; 
               List<Contact> conts = [select Id from Contact where Account.Id = ];   
               List<Attachment> accountAttachmentLst = new List<Attachment>();  
               for(Contact contact : conts ){
                   accountAttachmentLst.addAll([select name, body, parentid from Attachment where PARENTID =]);
               System.debug('DEBUG: Copy_Attachments_Accts_to_Oppr:: Opportunities ->'+opprs );
               System.debug('DEBUG: Copy_Attachments_Accts_to_Oppr:: Contact Attachment List ->'+accountAttachmentLst );       
                for (Attachment a : accountAttachmentLst ) {
                    System.debug('DEBUG: Copy_Attachments_Accts_to_Oppr:: Attachment ->';
                    System.debug('DEBUG: Copy_Attachments_Accts_to_Oppr:: Attachment ->'+a.parentid);
                    for(Opportunity opr : opprs ){
                       Attachment att = new Attachment(name =, body = a.body, parentid =; 
                       System.debug('DEBUG: Copy_Attachments_Accts_to_Oppr:: ->' );
                       System.debug('DEBUG: Copy_Attachments_Accts_to_Oppr:: attachments.parentid ->'+att.parentid);
                    //Attachment att = new Attachment(name =, body = a.body, parentid =; 
                   // attachmentsToDelete.add(att);
        System.debug('DEBUG: Copy_Attachments_Accts_to_Oppr:: attachmentsToInsert->'+attachmentsToInsert); 
        //System.debug('DEBUG: Copy_Attachments_Accts_to_Oppr:: attachmentsToDelete'+attachmentsToDelete); 
         if (attachmentsToInsert.size() > 0) {
             insert attachmentsToInsert;

         // Minimize storage requirements by cleaning up attachments
         /*if (attachmentsToDelete.size() > 0) {
             delete attachmentsToDelete;
   // }

Any suggestions to circumvent this problem will be much appreciated.


Jainam ContractorJainam Contractor
Hi Debendra,

There are couple of issues with your code:

1) You are using SOQL query within the FOR Loops (1st for loop), it will hit the governor limit of 100 SOQL/ transaction so better rectify that mistake.

2) You have initialized the List of Attachments in the FOR loop so everytime the FOR Loop will iterate, it will initialize a new list which is avoiding the Attachment to get attached to the Opportunity.

3) You are not checking whether Account Id and Opportunity Account Id are same before associating the Attachment to the Opportunity. So every attachment will associate with all Opportunity and eventually it will fail.

Please make those changes and check if it works.

Please let me know if you need any more assistance.

Jainam Contractor,
Salesforce Consultant,
Varasi LLC
Debendra Ray 8Debendra Ray 8
Hi Jainam,
Thanks for your response - While I'll definitely make changes recommended by yourself, however, please note that this trigger is working fine if the Account is updated manually after creation. Also, after creation of an account , the following DEBUG statements do not fetch any data :
               System.debug('DEBUG: Copy_Attachments_Accts_to_Oppr:: Opportunities ->'+opprs );
               System.debug('DEBUG: Copy_Attachments_Accts_to_Oppr:: Contact Attachment List ->'+accountAttachmentLst );      

Please, could you suggest any reason for this behaviour?