高章伟
2023-03-03 d8dc84a3d56df839895f1c417a4d9cbee763d262
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public without sharing class BatchSelectRepairPDFDelete {
    public void deletePDF(List<Repair__c> repList){
        List<String> attachmentNameList = new List<String>();
        List<Attachment> isGeneratedPDFList = new List<Attachment>();
        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<Repair__c> repList){
        PageReference pdfPage;
        List<Attachment> attachments = new List<Attachment>();
        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;
        }
}