public without sharing class BatchSelectRepairPDFDelete { public void deletePDF(List repList){ // 20231103 Lightning文件修改 Start //修改删除附件,改为删除文件(lightning改造) List attachmentNameList = new List(); List parentIdList = new List(); List isGeneratedPDFLinkList = new List(); List isGeneratedPDFList = new List(); for(Repair__c re : repList){ attachmentNameList.add(re.name + '_' + 'MaintenanceCommission.pdf'); parentIdList.add(re.Id); } isGeneratedPDFLinkList = [select ContentDocumentId from ContentDocumentLink where LinkedEntityId in: parentIdList]; List attachmentIdList = new List(); for (ContentDocumentLink PDFLink : isGeneratedPDFLinkList) { attachmentIdList.add(PDFLink.ContentDocumentId); } isGeneratedPDFList = [select Id from ContentDocument where Id in: attachmentIdList and Title in: attachmentNameList]; if(isGeneratedPDFList.size() > 0){ delete isGeneratedPDFList; } // List attachmentNameList = new List(); // List isGeneratedPDFList = new List(); // for(Repair__c re : repList){ // attachmentNameList.add(re.name + '_' + 'MaintenanceCommission.pdf'); // } // isGeneratedPDFList = [select id from Attachment where name IN:attachmentNameList]; // System.debug(LoggingLevel.INFO, '*** isGeneratedPDFList: ' + isGeneratedPDFList); // if(isGeneratedPDFList.size() > 0){ // delete isGeneratedPDFList; // } // 20231103 Lightning文件修改 End } public void createPDF(List repList){ // 20231103 Lightning文件修改 Start // PageReference pdfPage; // List attachments = new List(); // for(Repair__c re : repList){ // pdfPage = new PageReference('/apex/MaintenanceCommissionPDF?id=' + re.Id); // Blob pdfBody; // if(System.Test.isRunningTest()) { // pdfBody = blob.valueOf('Unit.Test'); // } else { // pdfBody = pdfPage.getContentAsPDF(); // } // Attachment attach = new Attachment(); // attach.Body = pdfBody; // attach.Name = re.name + '_' + 'MaintenanceCommission.pdf'; // attach.ParentId = re.id; // attachments.add(attach); // } // System.debug(LoggingLevel.INFO, '*** attachments: ' + attachments); // insert attachments; PageReference pdfPage; List versions = new List(); for(Repair__c re : repList){ pdfPage = new PageReference('/apex/MaintenanceCommissionPDF?id=' + re.Id); Blob pdfBody; if(System.Test.isRunningTest()) { pdfBody = blob.valueOf('Unit.Test'); } else { pdfBody = pdfPage.getContentAsPDF(); } ContentVersion version = new ContentVersion(); version.VersionData = pdfBody; version.Title = re.name + '_' + 'MaintenanceCommission.pdf'; version.ContentLocation = 's'; version.PathOnClient = re.name + '_' + 'MaintenanceCommission.pdf'; versions.add(version); } if (versions.size() > 0) { insert versions; } List idList = new List(); for (ContentVersion newVersion : versions) { idList.add(newVersion.Id); } versions = [select ContentDocumentId from ContentVersion where Id in: idList]; List links = new List(); Integer i = 0; for (ContentVersion newVersion : versions) { ContentDocumentLink link = new ContentDocumentLink(); link.ContentDocumentId = newVersion.ContentDocumentId; link.LinkedEntityId = repList[i].Id; link.ShareType = 'I'; link.Visibility = 'AllUsers'; links.add(link); i++; } if (links.size() > 0) { insert links; } // 20231103 Lightning文件修改 End } }