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
JFraileJFraile 

Error trying to create PDF

Hi.
I have a button wich invokes a VF page wich invokes an apex method that creates a new record and then invokes another method wich creates a PDF (using a VF renderas PDF and its controller) for this new record.

When I push the button the new record is created and a PDF is attached to it. (The record is an invoice, and the PDF is this invoice PDF.) The problem is that the PDF has no content at all, cannot even open it, and just has 9 bytes. But, once I've created the new record, if I run just the method that creates de PDF, a new PDF is attached to the invoice, this time with content in it.

debugging my code I've found where the error seems to be. The VF controller throws an exception when it tries to retrive data using the newly created record id, passed by page parameter, here is the code:

public with sharing class ControllerTestGetContent {      
        public List<OpportunityLineItem> productos;                
        public ControllerTestGetContent (ApexPages.Standardcontroller stdController){        	
        	}
public List<OpportunityLineItem> getProductos(){
        	string ide = ApexPages.currentPage().getParameters().get('Id');
        	system.debug('The invoice Id is: '+ide);// THIS DEBUG RETURNS THE RIGHT INVOICE ID
         try{        	
                Factura__c a=[select Oportunidad__c from Factura__c where
                Id=:Ide]; // BUT THIS OTHER THROWS A EXCEPTION System.VisualforceException: List has no rows for assignment to SObject

 

I've tried different ways to perform this query, with the same result.

The code that invokes the VF is this:

public class CrearFacturaPDF{
	public CrearFacturaPDF() {}
	public CrearFacturaPDF (ApexPages.StandardController controller){}
    public static boolean CrearPDF2(string ID_Fac){  
	[...]
	    PageReference ElPDF = Page.TestgetContent;    	
    	ElPDF.getParameters().put('id',ID_Fac);     
    	// create the new attachment
    	Attachment attach = new Attachment(); 
    	// the contents of the attachment from the pdf
    	Blob body; 
    	try { 
    	    // returns the output of the page as a PDF
    		body = ElPDF.getContent();    		    		
   		 } catch (VisualforceException e) {
    		body = Blob.valueOf('Some Text');    		
   		 }
The code that creates the new record and invokes the CrearPDF2 method is:
public with sharing class CrearFacturaProforma {
	public CrearFacturaProforma(ApexPages.StandardController controller) { }	
	public PageReference CrearProforma() {
		[...]
		insert FacturaProforma;               
        boolean done = CrearFacturaPDF.CrearPDF2(FacturaProforma.Id);                
        PageReference Volver_A_Detalle = new PageReference('/' + FacturaProforma.Id);
        Volver_A_Detalle.setRedirect(true);
        return Volver_A_Detalle;

It seems like it's not possible to create a new record and ,under the same running process, create a PDF for this new record. Is it that way? must it be done throght two separate execution processes?

Thanks.


 

 

 

    
                  
Thanks.