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 

how can I clone attachments and paste them into another record

I'm trying to clone attachments from a Record Type to another.
I have 2 record types (Prospects and accounts) these types of records belong to the account object. what is sought is that if a record of the record type Prospects has the same number of sap as a record with the record type accounts. Clone the attachments that are in the prospect register and paste them into the account register.  It needs to be a Apex class because I run this class sinze ();

public class Set_SapId {
public static void CloneAttachments{
    List<Accout>GetAccountId =[SELECT ONTAP__SAP_Number__c From Account WHERE RecordType.DeveloperName ='Account'];

    System.debug(GetAccountId);

    List<Accout>GetProspectId =[SELECT ONTAP__SAP_Number__c From Account WHERE RecordType.DeveloperName ='Prospect'];
    System.debug(GetProspectId);

    if(GetProspectId.ONTAP__SAP_Number__c= GetAccountId.ONTAP__SAP_Number__c){

Attachment fileAttachment = [SELECT SELECT Id,
                              Body,
                              Name,
                              Description,
                              ParentId
                              FROM Attachment
                              WHERE Id=:attachmentId] 


  Attachment newFile = fileAttachment.clone();
   // newFile.ParentId = paramParentId;
    //insert newFile;
    }
}


}

So far I have this code but I can not think of ideas to paste the cloned atachments in the account register. If someone could support me with that, I would appreciate it

 
Raj VakatiRaj Vakati
Try like below 
 
Attachment fileAttachment = [SELECT SELECT Id,
                              Body,
                              Name,
                              Description,
                              ParentId
                              FROM Attachment
                              WHERE Id=:attachmentId] 


  Attachment newFile = fileAttachment.clone();
   newFile.ParentId = fileAttachment.ParentId;
   newFile.Body = fileAttachment.Body;
   newFile.Name = fileAttachment.Name;
   newFile.Description = fileAttachment.Description;
   
   insert newFile;
    }

 
Rafael MottaRafael Motta
Actualy I try to do it  but Salesforce show a Follow exception:

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger ActualizaAttachmente caused an unexpected exception, contact your administrator: ActualizaAttachmente: execution of AfterUpdate caused by: System.QueryException: List has no rows for assignment to SObject: Class.Set_attachments.TESTMETOD: line 26, column 1