My_Test_Repo/force-app/main/default/classes/CopyAttachmentHandler.cls

69 lines
2.4 KiB
OpenEdge ABL

public class CopyAttachmentHandler {
public static void copyAtt(List<ProjectDemo__c>pdList){
try{
//releted opp id
Set<ID> OppId = new Set<ID>();
Set<ID> CdId = new Set<ID>();
// related attachment id of opp id
Set<ID> attId = new Set<ID>();
for(ProjectDemo__c pd1:pdList){
OppId.add(pd1.Opportunity_Name__c);
}
System.debug('@@@@@@ oppid'+OppId);
for(ContentDocumentLink ac:[SELECT Id,ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId IN:OppId]){
attId.add(ac.Id);
CdId.add(ac.ContentDocumentId);
}
System.debug('@@@@@@ Attid'+attId);
System.debug('@@@@@@ CdId'+CdId);
list<ContentVersion> newContentVersion=new list<ContentVersion>();
list<ContentDocumentLink> cdl=new list<ContentDocumentLink>();
List<ContentVersion> attachmentList = [SELECT Id, VersionData,Description, Title, ContentBodyId, ContentDocumentId FROM ContentVersion WHERE ContentDocumentId IN:CdId];
System.debug('@@@@@@ attachmentList'+attachmentList);
for(ContentVersion att: attachmentList) {
ContentVersion newFile = new ContentVersion();
newFile.Title = att.Title;
newFile.ContentBodyId = att.ContentBodyId;
newFile.Description=att.Description;
newContentVersion.add(newFile);
}
insert newContentVersion;
List<Id> CdIdid = new List<Id>();
for(ContentVersion cvv:newContentVersion){
CdIdid.add(cvv.ContentDocumentId);
}
System.debug('@@@@@@ new content documnet id'+CdIdid);
for(ProjectDemo__c pd:pdList){
ContentDocumentLink newLink= new ContentDocumentLink();
newLink.LinkedEntityId=pd.Id;
for(Id ccd:CdIdid){
newLink.ContentDocumentId=ccd;
}
cdl.add(newLink);
}
insert cdl;
}
catch(Exception e){
system.debug('@@@@ Error'+e);
}
}
}