19626
2023-10-31 fd9df05c48c09b4d7f39a3b401882bda226dedea
force-app/main/default/classes/RepairAndQISToPDFController.cls
@@ -57,7 +57,10 @@
        PageReference pdfPage ;
        
        List<Repair__c> updateRpr = new List<Repair__c>();
        List<Attachment> attachments = new List<Attachment>();
            // List<Attachment> attachments = new List<Attachment>();
            List<ContentVersion> cVersions = new List<ContentVersion>();
            List<ContentDocumentLink> cDocLinks = new List<ContentDocumentLink>();
            Map<String,String> res = new Map<String, String>();
        for(Repair__c re : repList){
            String generatedFields = re.GeneratedPDFField__c;
            if (checkFieldisGeneratedPDF('ProblemDescription__c',re)) {
@@ -75,15 +78,39 @@
            } 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';
                cVersions.add(version);
                res.put(version.Title, re.Id);
                // insert version;
                // 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;
            if (cVersions.size()>0) {
                insert cVersions;
            }
            List<Id> idList = new List<Id>();
            for (ContentVersion newVersion : cVersions) {
                idList.add(newVersion.Id);
            }
            cVersions = [select ContentDocumentId,Title from ContentVersion where Id in: idList];
            for (ContentVersion cv : cVersions) {
                ContentDocumentLink cdl = new ContentDocumentLink();
                cdl.ContentDocumentId = cv.ContentDocumentId;
                cdl.LinkedEntityId = res.get(cv.Title);
                cdl.ShareType = 'I';
                cdl.visibility = 'AllUser';
                cDocLinks.add(cdl);
            }
            insert cDocLinks;
            // insert attachments;
        update updateRpr;
        return null;
    }
@@ -94,7 +121,10 @@
        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>();
            List<ContentVersion> cVersions = new List<ContentVersion>();
            List<ContentDocumentLink> cDocLinks = new List<ContentDocumentLink>();
            Map<String,String> res = new Map<String, String>();
        PageReference pdfPage ;
        for(QIS_Report__c qis : qisReports){
@@ -126,16 +156,23 @@
                } 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;
                attachments.add(attach);
                    version.PathOnClient = substringApiName(qis.name) + '_' + 'Problem_' + apiName.substring(0,apiName.length()-3) + '.pdf'+'.pdf';
                    // attach.ParentId = qis.id;
                    // attachments.add(attach);
                    cVersions.add(version);
                    res.put(version.Title, qis.Id);
                    // insert version;
                if (generatedFields == null) {
                    qis.GeneratedPDFField__c = apiName;
                }else {
@@ -144,7 +181,24 @@
            }
            updateQis.add(qis);
        }
        insert attachments;
            if (cVersions.size()>0) {
                insert cVersions;
            }
            List<Id> idList = new List<Id>();
            for (ContentVersion newVersion : cVersions) {
                idList.add(newVersion.Id);
            }
            cVersions = [select ContentDocumentId,Title from ContentVersion where Id in: idList];
            for (ContentVersion cv : cVersions) {
                ContentDocumentLink cdl = new ContentDocumentLink();
                cdl.ContentDocumentId = cv.ContentDocumentId;
                cdl.LinkedEntityId = res.get(cv.Title);
                cdl.ShareType = 'I';
                cdl.visibility = 'AllUsers';
                cDocLinks.add(cdl);
            }
            insert cDocLinks;
            // insert attachments;
        update updateQis;
        
        return null;