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
Rafael MottaRafael Motta 

clone attachments in diferent RecordType

Hi all. I set this question but i can solve  my problem I have another Script I need to do in appex clase.

in the accounts object, I have 2 record types (Prospects and Accounts) and a field that has the same id in the 2 records Sap_number Request:

If the record with the record Type "account" has the same Sap_number that the record with the Record type "Prospectus", clone the attachments contained in the "Prospects" record and paste them into the registry Record Type "Account" I have already mentioned this question before but I still can not clone the attachments and paste them into the new record. my code is as follows:

Script to get attachments in Prospect:

//variables 
    Integer attachmentId;
    String attachmentName;
    Blob attachmentBody;
    List<attachment> attachmentsToClone;
    List<attachment> attachmentList;

    List<attachment> getAttachmentList = [SELECT Id, Body, Name, Description, lastModifiedDate, CreatedById, CreatedDate, OwnerId, CreatedBy.Name, ParentId
                      FROM Attachment WHERE ParentId IN (SELECT Id FROM Account WHERE RecordType.DeveloperName = 'Prospect') 
                      ORDER BY CreatedDate Desc];
    
    
       List<String> valueid  = new List<String>();
    For (attachment attach: getAttachmentList){
    valueid.add(attach.Id);
    }
        List<String> valuename  = new List<String>();
        For (attachment attach: getAttachmentList){
        valuename.add(attach.Name);
        }
            List<String> valuedescription  = new List<String>();
            For (attachment attach: getAttachmentList){
            valuedescription.add(attach.Description);
            }
                List<String> valueparent  = new List<String>();
                For (attachment attach: getAttachmentList){
                valueparent.add(attach.ParentId);
                }
                    List<Blob> valuebody  = new List<Blob>();
                    For (attachment attach: getAttachmentList){
                    valuebody.add(attach.Body);
                    }
                        List<String> valueownwrid  = new List<String>();
                        For (attachment attach: getAttachmentList){
                        valueownwrid.add(attach.OwnerId);
                        }

        System.debug('This is debug getattachment before to clone'+ getAttachmentList);
        System.debug('This is debug, attachmentId before to clone' + valueid);
        System.debug('This is debug, attachmentName before to clone' + valuename);
           System.debug('This is debug, attachmentBody before to clone' + valuebody); 
        System.debug('This is debug, attachmentParentId before to clone' + valueparent);
        System.debug('This is debug, attachmentOwnerId before to clone' + valueownwrid); 


This  is  my script to get Attachments in Accounts:

// obtener attachments en accounts
List<attachment> getAttachmentListacc = [SELECT Id, Body, Name, Description, lastModifiedDate, CreatedById, CreatedDate, OwnerId, CreatedBy.Name, ParentId
                      FROM Attachment WHERE ParentId IN (SELECT Id FROM Account WHERE RecordType.DeveloperName = 'Account') 
                      ORDER BY CreatedDate Desc];
    
       List<String> valueidacc  = new List<String>();
    For (attachment attach: getAttachmentListacc){
    valueid.add(attach.Id);
    }
        List<String> valuenameacc  = new List<String>();
        For (attachment attach: getAttachmentListacc){
        valuename.add(attach.Name);
        }
            List<String> valuedescriptionacc  = new List<String>();
            For (attachment attach: getAttachmentListacc){
            valuedescription.add(attach.Description);
            }
                List<String> valueparentacc  = new List<String>();
                For (attachment attach: getAttachmentListacc){
                valueparent.add(attach.ParentId);
                }
                    List<Blob> valuebodyacc  = new List<Blob>();
                    For (attachment attach: getAttachmentListacc){
                    valuebody.add(attach.Body);
                    }
                        List<String> valueownwridacc  = new List<String>();
                        For (attachment attach: getAttachmentListacc){
                        valueownwrid.add(attach.OwnerId);
                        }

        System.debug('This is debug Accounts, getattachment before to clone'+ getAttachmentListacc);
        System.debug('This is debug Accounts, attachmentId before to clone' + valueidacc);
        System.debug('This is debug Accounts, attachmentName before to clone' + valuenameacc);
           System.debug('This is debug Accounts, attachmentBody before to clone' + valuebodyacc); 
        System.debug('This is debug Accounts, attachmentParentId before to clone' + valueparentacc);
        System.debug('This is debug Accounts, attachmentOwnerId before to clone' + valueownwridacc);



This is my evaluation of Sap_number records in Boath RT:



 if(valueidacc != null && valuenameacc !=null && valuebodyacc != null){
        
        System.debug('This is debug Accounts, attachmentId If is Null' + valueidacc);
        System.debug('This is debug Accounts, attachmentName If is Null' + valuenameacc);
           System.debug('This is debug Accounts, attachmentBody If is Null' + valuebodyacc); 
        
        //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>();
        
        // hace el recuento de cuentas 
    for(Account acc:Accountlist){
    
     //Obtener las cuentas que tienen el record type y que sap number no esta vacio
        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);
            
        }
    }
    
        //obtiene las cuentas con el record type cuentas que tengan un numero sap igual al del Account
    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);
    }
        System.debug('lista de prospectos '+Accountlistprospect);
        System.debug('lista de account '+ Accountlistaccount);
    System.debug('lista de id de cuenta '+AccountlistId);
    System.debug('lista de sapnumber '+AccountlistSapnumber);



This is My insert of attacghments:

 if(Accountlistaccount!=null)
    {
   Attachment newFile = getAttachmentList.clone();
   newFile.ParentId = getAttachmentList.ParentId;
   newFile.Body = getAttachmentList.Body;
   newFile.Name = getAttachmentList.Name;
   newFile.Description = getAttachmentList.Description;
   
   insert newFile;




This is a complete Script.

public class Set_attachments {
public static void TESTMETOD(list<Account>Accountlist){
   
    //variables 
    Integer attachmentId;
    String attachmentName;
    Blob attachmentBody;
    List<attachment> attachmentsToClone;
    List<attachment> attachmentList;

    List<attachment> getAttachmentList = [SELECT Id, Body, Name, Description, lastModifiedDate, CreatedById, CreatedDate, OwnerId, CreatedBy.Name, ParentId
                      FROM Attachment WHERE ParentId IN (SELECT Id FROM Account WHERE RecordType.DeveloperName = 'Prospect') 
                      ORDER BY CreatedDate Desc];
    
    
       List<String> valueid  = new List<String>();
    For (attachment attach: getAttachmentList){
    valueid.add(attach.Id);
    }
        List<String> valuename  = new List<String>();
        For (attachment attach: getAttachmentList){
        valuename.add(attach.Name);
        }
            List<String> valuedescription  = new List<String>();
            For (attachment attach: getAttachmentList){
            valuedescription.add(attach.Description);
            }
                List<String> valueparent  = new List<String>();
                For (attachment attach: getAttachmentList){
                valueparent.add(attach.ParentId);
                }
                    List<Blob> valuebody  = new List<Blob>();
                    For (attachment attach: getAttachmentList){
                    valuebody.add(attach.Body);
                    }
                        List<String> valueownwrid  = new List<String>();
                        For (attachment attach: getAttachmentList){
                        valueownwrid.add(attach.OwnerId);
                        }

        System.debug('This is debug getattachment before to clone'+ getAttachmentList);
        System.debug('This is debug, attachmentId before to clone' + valueid);
        System.debug('This is debug, attachmentName before to clone' + valuename);
           System.debug('This is debug, attachmentBody before to clone' + valuebody); 
        System.debug('This is debug, attachmentParentId before to clone' + valueparent);
        System.debug('This is debug, attachmentOwnerId before to clone' + valueownwrid); 
    
    
    // obtener attachments en accounts
List<attachment> getAttachmentListacc = [SELECT Id, Body, Name, Description, lastModifiedDate, CreatedById, CreatedDate, OwnerId, CreatedBy.Name, ParentId
                      FROM Attachment WHERE ParentId IN (SELECT Id FROM Account WHERE RecordType.DeveloperName = 'Account') 
                      ORDER BY CreatedDate Desc];
    
       List<String> valueidacc  = new List<String>();
    For (attachment attach: getAttachmentListacc){
    valueid.add(attach.Id);
    }
        List<String> valuenameacc  = new List<String>();
        For (attachment attach: getAttachmentListacc){
        valuename.add(attach.Name);
        }
            List<String> valuedescriptionacc  = new List<String>();
            For (attachment attach: getAttachmentListacc){
            valuedescription.add(attach.Description);
            }
                List<String> valueparentacc  = new List<String>();
                For (attachment attach: getAttachmentListacc){
                valueparent.add(attach.ParentId);
                }
                    List<Blob> valuebodyacc  = new List<Blob>();
                    For (attachment attach: getAttachmentListacc){
                    valuebody.add(attach.Body);
                    }
                        List<String> valueownwridacc  = new List<String>();
                        For (attachment attach: getAttachmentListacc){
                        valueownwrid.add(attach.OwnerId);
                        }

        System.debug('This is debug Accounts, getattachment before to clone'+ getAttachmentListacc);
        System.debug('This is debug Accounts, attachmentId before to clone' + valueidacc);
        System.debug('This is debug Accounts, attachmentName before to clone' + valuenameacc);
           System.debug('This is debug Accounts, attachmentBody before to clone' + valuebodyacc); 
        System.debug('This is debug Accounts, attachmentParentId before to clone' + valueparentacc);
        System.debug('This is debug Accounts, attachmentOwnerId before to clone' + valueownwridacc); 
    
    if(valueidacc != null && valuenameacc !=null && valuebodyacc != null){
        
        System.debug('This is debug Accounts, attachmentId If is Null' + valueidacc);
        System.debug('This is debug Accounts, attachmentName If is Null' + valuenameacc);
           System.debug('This is debug Accounts, attachmentBody If is Null' + valuebodyacc); 
        
        //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>();
        
        // hace el recuento de cuentas 
    for(Account acc:Accountlist){
    
     //Obtener las cuentas que tienen el record type y que sap number no esta vacio
        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);
            
        }
    }
    
        //obtiene las cuentas con el record type cuentas que tengan un numero sap igual al del Account
    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);
    }
        System.debug('lista de prospectos '+Accountlistprospect);
        System.debug('lista de account '+ Accountlistaccount);
    System.debug('lista de id de cuenta '+AccountlistId);
    System.debug('lista de sapnumber '+AccountlistSapnumber);
        
        
    if(Accountlistaccount!=null)
    {
   Attachment newFile = getAttachmentList.clone();
   newFile.ParentId = getAttachmentList.ParentId;
   newFile.Body = getAttachmentList.Body;
   newFile.Name = getAttachmentList.Name;
   newFile.Description = getAttachmentList.Description;
   
   insert newFile;
        //System.debug(valuebody);
        //attachment ga = getAttachmentListacc;
        //Insert a IN:Attachment WHERE valueparentacc==AccountlistId;
    }
 /* 
    
    //obtener Attachments
    Map<Attachment,id> MapaaccAttachment = new Map<Attachment, id>();
    
    // hace el recuento de cuentas 
    for(Account acc:Accountlist){
        
        //Obtener las cuentas que tienen el record type y que sap number no esta vacio
        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);
            
        }
    }
    
    //obtiene las cuentas con el record type cuentas que tengan un numero sap igual al del Account
    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);
    }
    
    //obtiene el attachment
    For(Attachment att:[Select id,Parentid from Attachment WHERE id in:AccountlistId]){
        MapaaccAttachment.put(att, att.ParentId);
    }
    
    System.debug(Accountlistprospect);
    System.debug(AccountlistId);
    System.debug(AccountlistSapnumber);*/
    
  
   /*If(AccountlistSapnumber==AccountlistSapnumber){
        
        System.debug(MapaaccAttachment);
        
    }*/
}:


Just I need Clone Attachments and set in the anoter RecordType Field

whats is wrong whit my screept or how i cant set those attachments in Account Record Type

Best Regards