function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AvisheKAvisheK 

PDF file Creation from a scheduled class

Hi,

 

I need to create a pdf file and insert it to the attachment object as record. This would fire as a scheduled class and not on a button click in a Visualforce page.

The code is as below:

 

global class OrderDispatchToTopcall implements Schedulable{
/*
        Author            : Cognizant
        Functionality     : This is a helper class is for the creation of the Order Dispatch pdf file and attach to the batch object.
                            The class acts as a controller class for the pdf generation.
                            Fetches data from the objects like Purchase order, Job Elements, Supplier
        Create Date       : 15 May 2010
        Change History    :
        Modified Date     :
    */
global void execute(SchedulableContext SC) {
PageReference pdf =  Page.MerchantDeliveryChecklist;
     pdf.getParameters().put('id', 'a1CR000000044H8');
     pdf.getParameters().put('pageType', 'NCHODC');
     pdf.setRedirect(true);
     Blob b=pdf.getContentAsPDF();
    
     Attachment attachment=new Attachment(ParentId='a1CR000000044H8',body=b,ContentType='application/pdf',Name='test.pdf');
     insert  attachment;
}
}

global class OrderDispatchToTopcall implements Schedulable{

 global void execute(SchedulableContext SC) {

 PageReference pdf =  Page.MerchantDeliveryChecklist;    

pdf.getParameters().put('id', 'a1CR000000044H8');    

 pdf.getParameters().put('pageType', 'NCHODC');    

 pdf.setRedirect(true);    

 Blob b=pdf.getContentAsPDF();      

  Attachment attachment=new Attachment(ParentId='a1CR000000044H8',body=b,ContentType='application/pdf',Name='test.pdf');    

insert  attachment;

 }

}

 

When this class runs it creates an attachment record but it is in a invalid format.

But when I call the same PDF creation code from button on a visualforce page, the attachment is created and it is a valid file.

 

Can somebody help me why I am not able to create a valid pdf file when the code runs from a schedular

 

ahab1372ahab1372

there are issues with getContent() when it is called from a testMethod, maybe the same bug applies to scheduled classes and getContentAsPDF as well?

 

See my post here:

http://community.salesforce.com/t5/Visualforce-Development/getContent-fails-when-called-from-testMethod/m-p/182645

 

I cannot see any obvious mistake in your code. One thing you can try is to change the pdf VF page to renderAs="pdf" and then use getContent instead of getContentAsPDF.

 

Did you log a case?

TechiesTechies

u cant use getcontent and getContentAsPDF   methods in a class which is scheduled..