I have to save the Dynalmically generated PDF from VFP on the Order Object

I am trying to attach PDF in contructor call only

For the given code I am getting error : Too many Content Calls

public Cls_Invoice(ApexPages.StandardController stdController) {

        stdController.addFields(new List<String>{'Case__c'});
        caseId = ((Order__c)stdController.getRecord()).Case__c;
        orderId = stdController.getId();

        OrderProductList = [Select Id , Name, Quantity__c, Price__c,  Packaging__c, Total_Price__c, Total_Weight__c from OrderProduct__c where Order__r.Id =: orderId ];       
        count = OrderProductList.Size();

        // Insert current page as PDF attachment to the Order.        
        System.PageReference currentPage = ApexPages.currentPage();
        Blob pdfData = currentPage.getContentAsPDF();
        Attachment attach = new Attachment();
        attach.ParentId = orderId;
        attach.Name = 'Invoice_'+System.now()+'.pdf';
        attach.Body = pdfData ;
        insert attach;
        //return null; 

You can't use getContentAsPDF nor do any DML statements inside your constructor. You should move that code out in a separate method that you will call explicitly. Either call the method from the action attribute on your VF page or via commandbutton etc.

  • can we have something like attach.Parent = apex.CurrentPage.toBlob(); Aug 7 '14 at 7:19
  • You can still call getCurrentPage and other methods in your constructor with the intent to only initialise your local variables, but doing getContent and other DML stuff need to go in another method. Aug 7 '14 at 7:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.