+ Start a Discussion
Rafael MottaRafael Motta 

How i can clone attachments since one record to another in the same object

I have 2 record types in Accounts object, I need copy since Prospect RecordType to Accounts RecordType if boath record have the same "Sap number" (Custom field) i need to copy Attachments from Prospect to Account. in my code I get ids, Developer names and list of records but  I have no Idea how I can clone the attachments from prospect and put it in account record do you have any similar code that could helps me ? or any solution to my problem.

Apex Class:

public class Set_attachments {
public static void TESTMETOD(list<Account>Accountlist){
    //obtener lista de  prospectos
    List<Account>Accountlistprospect=new List<Account>();
    //obtener lista de cuentas
    List<Account>Accountlistaccount=new List<Account>();
    //obtener Id de las cuentas 
    Set<id>AccountlistId=new Set<id>();
    //obtener numero de sap
    Set<String>AccountlistSapnumber=new Set<String>();
    //obtener Attachments
    Map<Attachment,id> MapaaccAttachment = new Map<Attachment, id>();
    // hace el recuento de cuentas 
    for(Account acc:Accountlist){
        
        //Record Type prospectos
        if((acc.RecordTypeId==Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Prospect').getRecordTypeId()) && acc.ONTAP_SAP_Number__c!=null){
            Accountlistprospect.add(acc);
            AccountlistId.add(acc.id);
            AccountlistSapnumber.add(acc.ONTAP_SAP_Number__c);
            
        }
    }
    For(Account acc:[Select id, ONTAP_SAP_Number__c from Account WHERE ONTAP_SAP_Number__c in:AccountlistSapnumber AND RecordTypeId=:Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Account').getRecordTypeId()]){
        Accountlistaccount.add(acc);
    }
    For(Attachment att:[Select id,Parentid from Attachment WHERE id in:AccountlistId]){
        MapaaccAttachment.put(att, att.ParentId);
    }
    
 
        
    }
    
}

    

        
     
pankul guptapankul gupta
Hi Rafael,

Please find the below links for your reference how to copy the attachments from one record to another.
https://developer.salesforce.com/forums/?id=906F00000008zRaIAI

https://salesforce.stackexchange.com/questions/42700/copying-attachments-from-object-to-object-trigger


Please let me know if you are still not able to. Will look into your code then.

Regards,
Pankul Gupta