public without sharing class BatchSelectRepairPDFDelete { public void deletePDF(List repList){ 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; } } public void createPDF(List repList){ PageReference pdfPage; List attachments = new List(); for(Repair__c re : repList){ pdfPage = new PageReference('/apex/MaintenanceCommissionPDF?id=' + re.Id); Blob pdfBody; if(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; } }