yumenghui
2023-08-04 0a3f9325599467015aecc4af7543dd10fe70ec56
force-app/main/default/classes/RepairAndQISToPDFController.cls
@@ -57,7 +57,7 @@
        PageReference pdfPage ;
        
        List<Repair__c> updateRpr = new List<Repair__c>();
        List<Attachment> attachments = new List<Attachment>();
        // List<Attachment> attachments = new List<Attachment>();
        for(Repair__c re : repList){
            String generatedFields = re.GeneratedPDFField__c;
            if (checkFieldisGeneratedPDF('ProblemDescription__c',re)) {
@@ -75,15 +75,29 @@
            } else {
                pdfBody = pdfPage.getContentAsPDF();
            }
            Attachment attach = new Attachment();
            attach.Body = pdfBody;
            attach.Name = re.name + '_' + 'Problem_Discription.pdf';
            attach.ParentId = re.id;
            attachments.add(attach);
            ContentVersion version = new ContentVersion();
            version.VersionData = pdfBody;
            version.Title = re.name + '_' + 'Problem_Discription.pdf';
            version.PathOnClient = re.name + '_' + 'Problem_Discription.pdf'+'.pdf';
            insert version;
            Id docId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id = :version.Id].ContentDocumentId;
            ContentDocumentLink cdl = new ContentDocumentLink();
            cdl.ContentDocumentId = docId;
            cdl.LinkedEntityId = re.id;
            cdl.ShareType = 'I';
            cdl.visibility = 'AllUser';
            insert cdl;
            // Attachment attach = new Attachment();
            // attach.Body = pdfBody;
            // attach.Name = re.name + '_' + 'Problem_Discription.pdf';
            // attach.ParentId = re.id;
            // attachments.add(attach);
            re.GeneratedPDFField__c = 'ProblemDescription__c';
            updateRpr.add(re);
        }
        insert attachments;
        // insert attachments;
        update updateRpr;
        return null;
    }
@@ -94,7 +108,7 @@
        System.debug('1111111111111111111111111111111111111111111111111generateAttachment执行了-----------');
        List<QIS_Report__c> updateQis = new List<QIS_Report__c>();
        List<Attachment> attachments = new List<Attachment>();
        // List<Attachment> attachments = new List<Attachment>();
        PageReference pdfPage ;
        for(QIS_Report__c qis : qisReports){
@@ -126,16 +140,30 @@
                } else {
                   pdfBody = pdfPage.getContentAsPDF();
                }
                Attachment attach = new Attachment();
                attach.Body = pdfBody;
                ContentVersion version = new ContentVersion();
                version.VersionData = pdfBody;
                // Attachment attach = new Attachment();
                // attach.Body = pdfBody;
                if ('problem_detail_photo__c'.equals(apiName)) {
                    attach.Name =  substringApiName(qis.name) + '_Problem_Discription.pdf';
                    // attach.Name =  substringApiName(qis.name) + '_Problem_Discription.pdf';
                    version.Title = substringApiName(qis.name) + '_Problem_Discription.pdf';
                }else {
                    attach.Name = substringApiName(qis.name) + '_' + 'Problem_' + apiName.substring(0,apiName.length()-3) + '.pdf';
                    // attach.Name = substringApiName(qis.name) + '_' + 'Problem_' + apiName.substring(0,apiName.length()-3) + '.pdf';
                    version.Title = substringApiName(qis.name) + '_' + 'Problem_' + apiName.substring(0,apiName.length()-3) + '.pdf';
                }
                attach.ParentId = qis.id;
                version.PathOnClient = substringApiName(qis.name) + '_' + 'Problem_' + apiName.substring(0,apiName.length()-3) + '.pdf'+'.pdf';
                // attach.ParentId = qis.id;
                // attachments.add(attach);
                insert version;
                Id docId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id = :version.Id].ContentDocumentId;
                ContentDocumentLink cdl = new ContentDocumentLink();
                cdl.ContentDocumentId = docId;
                cdl.LinkedEntityId = qis.id;
                cdl.ShareType = 'I';
                cdl.visibility = 'AllUser';
                insert cdl;
                attachments.add(attach);
                if (generatedFields == null) {
                    qis.GeneratedPDFField__c = apiName;
                }else {
@@ -144,7 +172,7 @@
            }
            updateQis.add(qis);
        }
        insert attachments;
        // insert attachments;
        update updateQis;
        
        return null;