• Rob Goodman
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies
I am facing the issue where PDF getting generated from VF page are blank. 
I do not have the corresponding critical update activated to treat getContentAsPDF() Methods as Callouts. 
The method getContentAsPDF() is invoked from a method SendPDF on custom controller class. And this SendPDF method is getting called as an action from a VF page.
Below is the code of this method. After upsert Survey, trigger class gets invoked on Survey object.

public static void SendPDF()
{
String AccountId = ApexPages.currentPage().getParameters().get('id');
String SurveyId = ApexPages.currentPage().getParameters().get('survey');

Survey__c[] Survey = [ SELECT Id, Name, Status__c, Send_PDF__c
FROM Survey__c
WHERE Id = :SurveyId LIMIT 1 ];

if (Survey[0].Status__c.equals('Complete') && 
(Survey[0].Send_PDF__c == null || ! Survey[0].Send_PDF__c.equals('Sent')))
{
PageReference pdfReport = Page.SurveyPg;

pdfReport.getParameters().put('id', AccountId);
pdfReport.getParameters().put('survey', SurveyId );
//System.debug(pdfReport);

Blob PDFBlob = pdfReport.getcontentAsPdf();
String PDFName = 'Survey' + Survey[0].Name + '.pdf';

Attachment attachment = new Attachment();
attachment.Body = PDFBlob;
attachment.Name = PDFName;
attachment.ParentId = SurveyId;
insert attachment;

Survey[0].Send_PDF__c = 'Sent';
upsert Survey;
}
}

We are not sending the PDF as email but instead attaching it to the survey record.
I am not directly calling the method from trigger and also do not have the critical update activated but still facing the issue.
Is there something else I need to do to make sure that getContentAsPDF() works correctly?
My usecase is, the BatchApex creates a record on QNews__c object whenever a new article is published or new versioned.
In execute method of BatchApex i wrote a code to send email with pdf as attachment. But i'm getting an email with empty pdf file. Any help???

My Execute Batch Code:
public class QNewsUpdateBatch {  
    public class QnewsWrapper{
        public String strArticleId{get;set;}
        public String strArticleTitle{get;set;}
        public String strArticleType{get;set;}
        public String strArticleURL{get;set;}
        public String strArticelVersionNumber{get;set;}
        public String strDataCategory{get;set;}
        public String strDataCategoryGroupName{get;set;}
        public String strDataCategorySelectionId{get;set;}
        public Boolean blnFederalCompliance{get;set;}
        public DateTime lastPublishedDate{get;set;}
        public Boolean blnStateCompliance{get;set;}
        public String strVersionComments{get;set;}
        public String strVersionHistoryId{get;set;}
        public String strVersionHistoryParentid{get;set;}
        public String strVrsionHistoryVersionId{get;set;} 
        public boolean blnBestPractice{get;set;}
        public String strDataCategoryParentId{get;set;}
    }
    public void insertQnewsData(String strArticleType){
        
        Map<Id,Id> VersionParentMap = new Map<Id,Id>();
        Map<Id,Id> VersionIdMap = new Map<Id,Id>();
        List<SObject> lst_versionhistory = Database.Query('SELECT VersionNumber,VersionId,ParentSobjectType,ParentId,Id,EventType,CreatedDate FROM '+strArticleType+'__VersionHistory  where (EventType=\'KavPublishedNew\' or EventType=\'KavPublished\') and ParentSobjectType=\''+strArticleType+'\'');  
        List<QnewsWrapper> listQnews = new List<QnewsWrapper>();
        for(SObject obj:lst_versionhistory){
            VersionParentMap.put((Id)obj.get('versionId'),(Id)obj.get('ParentId'));
            VersionIdMap.put((Id)obj.get('versionId'),(Id)obj.get('Id'));
        }         
        if(VersionParentMap!=null && VersionParentMap.size()>0){
            Set<Id> VersionParentSet = VersionParentMap.keySet();
            List<SObject> lst_DataCategory = Database.Query('Select Id,DataCategoryName,DataCategoryGroupName,ParentId from '+strArticleType+'__DataCategorySelection where ParentId IN :VersionParentSet');
            for(SObject obj:lst_DataCategory){
                        QnewsWrapper qnewsObj = new QnewsWrapper();
                        
                        qnewsObj.strDataCategory = (String)obj.get('DataCategoryName');
                        qnewsObj.strDataCategoryGroupName = (String)obj.get('DataCategoryGroupName');
                        qnewsObj.strDataCategorySelectionId = (String)obj.get('Id');
                        qnewsObj.strDataCategoryParentId = (String)obj.get('ParentId');
                        
                        listQnews.add(qnewsObj);
            }
            List<SObject> lst_ArticleKAV = Database.Query('Select Id,Version_Comments__c,VersionNumber,UrlName,Title,State_Compliance__c,LastPublishedDate,KnowledgeArticleId,Federal_Compliance__c,Best_Practice__c,ArticleType From '+strArticleType+'__kav where Id IN :VersionParentSet');
            for(SObject obj:lst_ArticleKAV){
                for(QnewsWrapper qnewsObj:listQnews){
                    if(qnewsObj.strDataCategoryParentId == obj.get('Id')){
                        qnewsObj.strVersionComments = (String)obj.get('Version_Comments__c');
                        qnewsObj.strArticelVersionNumber = String.valueOf(obj.get('VersionNumber'));
                        qnewsObj.strArticleURL = (String)obj.get('UrlName');
                        qnewsObj.strArticleTitle = (String)obj.get('Title');
                        qnewsObj.blnStateCompliance = (Boolean)obj.get('State_Compliance__c');
                        qnewsObj.lastPublishedDate = (DateTime)obj.get('LastPublishedDate');
                        qnewsObj.strArticleId = (String)obj.get('KnowledgeArticleId');
                        qnewsObj.blnFederalCompliance = (Boolean)obj.get('Federal_Compliance__c');
                        qnewsObj.blnBestPractice = (Boolean)obj.get('Best_Practice__c');
                        qnewsObj.strArticleType = (String)obj.get('ArticleType');
                        qnewsObj.strVersionHistoryParentid = (String)VersionParentMap.get((Id)obj.get('Id'));
                        qnewsObj.strVersionHistoryId = (String)VersionIdMap.get((Id)obj.get('Id'));
                        qnewsObj.strVrsionHistoryVersionId = (String)obj.get('Id');
                    }
                }
            }
        }
        Set<String> existingQNewsVersionsId = new Set<String>();
        for(QNews__c obj:[Select Id,Version_History_Version_Id__c from QNews__c]){
            existingQNewsVersionsId.add(obj.Version_History_Version_Id__c);
        }
        
        List<QNews__c> qnewsToInsert = new List<QNews__c>();
        
        for(QnewsWrapper obj:listQnews){
            if(!existingQNewsVersionsId.contains(obj.strVrsionHistoryVersionId)){   
                QNews__c qnewsObj = new QNews__c();
                qnewsObj.Data_Category_Selection_Id__c = obj.strDataCategorySelectionId;
                qnewsObj.Data_Category_Group__c = obj.strDataCategoryGroupName;
                qnewsObj.Data_Category__c = obj.strDataCategory;
                qnewsObj.Article_ID__c = obj.strArticleId;
                qnewsObj.Article_URL__c = obj.strArticleURL;
                qnewsObj.Article_Title__c = obj.strArticleTitle;
                qnewsObj.Article_Version_Number__c = obj.strArticelVersionNumber;
                qnewsObj.Version_Comments__c = obj.strVersionComments;
                qnewsObj.Last_Published__c = obj.lastPublishedDate;
                qnewsObj.Article_Type__c = obj.strArticleType;
                qnewsObj.Federal_Compliance__c = obj.blnFederalCompliance;
                qnewsObj.State_Compliance__c = obj.blnStateCompliance;
                qnewsObj.Best_Practice__c = obj.blnBestPractice;
                qnewsObj.Version_History_Id__c = obj.strVersionHistoryId;
                qnewsObj.Version_History_Version_Id__c = obj.strVrsionHistoryVersionId;
                qnewsObj.Version_History_Parent_Id__c = obj.strVersionHistoryParentid;
                string filename = qnewsObj.Article_Title__c+'_'+qnewsObj.Article_Version_Number__c+'.pdf';
                string newarticleID = String.valueOf(qnewsObj.Article_ID__c);
                String emailbody='This attachment is the PDF copy of Article:  '+qnewsObj.Article_Title__c+' with version number '+qnewsObj.Article_Version_Number__c;                  GETPDFContentnew.sendEmail('vijaku@vsp.com',qnewsObj.Data_Category__c,emailbody,newarticleID,filename,qnewsObj.Article_Title__c,qnewsObj.Article_Type__c);  
   
                qnewsToInsert.add(qnewsObj);      
            }
        }
 if(qnewsToInsert!=null && qnewsToInsert.size()>0){
            insert qnewsToInsert;
        }    
    }
}

Class to send email:
Global class GETPDFContentnew{
    global static void sendEmail(String EmailIdCSV, String Subject, String body,string newID,string attachmentfilename,string ArticleTitle,string ArticleType) {
    List<String> EmailIds = EmailIdCSV.split(',');
        PageReference ref = null;      
        if(ArticleType=='Multi_Topic__kav'){
             
            ref = Page.Multi_Topic_PDF; 
          }
         if(ArticleType=='Additional_Benefits__kav'){
            ref = Page.AdditionalBenefits_PDF; 
          }
         if(ArticleType=='Core_Benefit__kav'){
            ref = Page.CoreBenefit_PDF; 
          }
         if(ArticleType=='Multi_Topic_Details__kav'){
            ref = Page.MultiTopicDetails_PDF; 
          }
         if(ArticleType=='Open_Enrollment__kav'){
            ref = Page.OpenEnrollment_PDF; 
          }
         if(ArticleType=='Single_Topic__kav'){
            ref = Page.SingleTopic_PDF; 
          }
        ref.getParameters().put('id',newID); 
            ref.setRedirect(true);
        Blob b = ref.getContentAsPDF();
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        Messaging.EmailFileAttachment efa1 = new Messaging.EmailFileAttachment();
        efa1.setFileName(attachmentfilename);
        efa1.setBody(b);
        String addresses;
        email.setSubject( Subject );
        email.setToAddresses(EmailIds);
        email.setPlainTextBody(Body);
        email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa1});
        Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
    }
}