Search code examples
salesforceapex-codesalesforce-lightning

How to Invoke VF page render as PDF from batch Apex


I want to call the vf page controller from an Batch apex. but i am getting the following error enter image description here

VF page controller

public with sharing class QuoteToPDFControllerdownload { ....

public QuoteToPDFControllerdownload() {

}

public QuoteToPDFControllerdownload(ApexPages.StandardController controller) {
    
    quoteRecord = [
            SELECT ID, Name, OpportunityId, Status, Subtotal, Design_Basis__c, Exclusions__c,
                    Special_Notes__c, Account.Company__r.Address_1__c, Account.Company__r.Address_2__c,
                    Account.Company__r.Address_3__c, Account.Company__r.Address_4__c,Account.CustomerCodeIFS__c , Tax, Discount,
                    Global_Discount__c, Absolute_Discount__c, TotalPrice, Contact_Info__c,
                    Opportunity.Salesman__r.Name, Opportunity.Salesman__r.MobilePhone, Opportunity.Salesman__r.Email,
                    Opportunity.Company__r.Address_1__c, Opportunity.Company__r.Address_2__c,
                    Opportunity.Company__r.Address_3__c, Opportunity.Company__r.Address_4__c,
                    Opportunity.Company__r.Name, Opportunity.Company__r.Company__c,
                    Opportunity.New_Customer_Name__c,Opportunity.New_Customer_address__c,Opportunity.New_Customer_Address_2__c,Opportunity.New_Customer_Address_3__c, Opportunity.Name, Opportunity.CreatedDate
            FROM Quote
            WHERE Id = :controller.getId()
    ];

String fileName = 'Quote_' + quoteRecord.Opportunity.Name + '.pdf'; ApexPages.currentPage().getHeaders().put('content-disposition', 'attachment; filename=' + fileName); }

VF page

<apex:page standardController="Quote" extensions="QuoteToPDFControllerdownload" showHeader="false" standardStylesheets="false" applyBodyTag="false" applyHtmlTag="false" docType="html-5.0" action="{! saveToPdf }" readOnly="false" renderAs="PDF">

.....

</apex:page>

Batch Class

public class QuoteToPDFBatchDownload implements Database.Batchable, Database.Stateful {

public Integer recordsProcessed = 0;
public Database.QueryLocator start(Database.BatchableContext bc) {
    
    return Database.getQueryLocator([
        SELECT ID, Opportunity_Number__c FROM Quote WHERE SentToIFS__c =TRUE    
    ]);

}

public void execute(Database.BatchableContext bc, List scope){

    for (Quote qt : scope) {
        // PageReference pr = New PageReference('/apex/culliganReortDownload?id=' + qt.id ); 
        ApexPages.StandardController sc = new ApexPages.StandardController(qt);  
        QuoteToPDFControllerdownload qd = new QuoteToPDFControllerdownload(sc);
          recordsProcessed = recordsProcessed + 1;
    
}
}

public void finish(Database.BatchableContext bc){
     System.debug(recordsProcessed + ' records processed. Ayesha!');
}

}


Solution

  • I suggest you can try using the following statement, in Apex, in a custom controller, to access the id which you are passing via Batch class:

    ApexPages.currentPage().getParameters().get('id');
    

    Please let me know if it helps you out.

    Thanks.