You need to sign in to do that
Don't have an account?

Getcontentaspdf() generates empy pdf
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});
}
}
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});
}
}
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_System_PageReference_getContentAsPDF.htm
the below link has a work around. you have to expose your email method as REST and then call it from your batch apex.
http://www.jitendrazaa.com/blog/salesforce/apex/send-email-with-generated-pdf-as-attachment-from-trigger/
In my solution, i'm calling a method which has getcontentaspdf() in Batch apex. i checked debug logs, all the code gets executed but pdf file is empty, any other suggestions?
to send the email as provided by jitendra