I have created a VF page for generating invoice and from that VF page i am invoking a PDF VF page. In invoice VF page i saved the invoice and pass the parameter to PDF page and trying to display it. I have a Print button to call the PDF page. <apex:commandButton onclick="openPrint()" value="Print" rendered="{!ShowHidePrint}" reRender="buttonblock"/> on the click of the print button it call a javascript from where it get redirect to PDF Page.

   ` <script>
        function openPrint()



their is a method called conform invoice which is used to save invoice and in the confor invoice method i am populating the {!sfdcBaceURL} the url to PDF page. Till here it works properly. Now i need to attach the generated PDF to the generated invoice. for that i created a method called attach and call the method in VF conform invoice method.

public PageReference attachPDF()
    system.debug('inside attachant  ');
    system.debug('invoice id  ' + invCrdNoteforInsert.id );
    system.debug('Invoice No in attachment ' + invCrdNoteforInsert.Name);
    PageReference redirect = new PageReference (sfdcBaseURL);
    /*redirect.getParameters().put('invId', invCrdNoteforInsert.id );    
    redirect.getParameters().put('expId', expID );    
    redirect.getParameters().put('grandT', string.valueof(grandTotal ));    
    redirect.getParameters().put('ExpgrandTotal', string.valueof(ExpgrandTotal) );    
    redirect.getParameters().put('TaxFlagAmt', string.valueof(TaxFlagAmt) );    
    redirect.getParameters().put('pjtName', project );   */
    //return redirect;
    system.debug('redirect body ' + redirect);
    insertedInvoiceId = invCrdNoteforInsert.id ;

    applicationpdf = new Attachment();
    system.debug('redirect body after attatchent' + redirect);

     system.debug('redirect body after attatchent Body' + redirect);
    applicationpdf.ContentType = '.pdf';
    applicationpdf.Name = 'Invoice - ' + String.valueof(Datetime.now());
    applicationpdf.Name += '.pdf';
    system.debug('inserted Invoice Id  ' + insertedInvoiceId);
    applicationpdf.ParentId = insertedInvoiceId;
    applicationpdf.Body = redirect.getContentAsPdf();
    insert applicationpdf;
    system.debug('inserted Invoice ' + applicationpdf);        
    insertedInvoiceId = null;            
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info, 'PDF has been attached!'));   
    return null;            

but when i call this method i am getting a error called list has no row to assign. The invoice is not generated and it is getting roll backed. Can any one guide me what is the issue for getting the roll back when i try to attach the pdf. It works when i created a new command button and calling the attach method. The problem accrues when attach method called from conform invoice method. Does any body know what is the issue and how to solve it.

1 Answer 1


I've hit this a few times in the past - as far as I've been able to tell, the problem is that the PDF generation happens in a separate transactional context to that of your Visualforce page. As you are inserting the invoice and then immediately trying to generate the PDF, the transaction has not yet completed so the invoice isn't accessible to other requests. Thus when the code to generate the PDF executes, the invoice doesn't yet exist.

The way I've handled this in the past is via browser techniques rather than Visualforce. For example, as part of the save method I set the value of a property on my controller, doAttach, to true. Then in my page I have some javascript which is only rendered into the page if doAttach is true, and this invokes another action method that generates the PDF, attaches it to the new object, sets doAttach to false and then refreshes page (or maybe a view page for the newly created object). Its slightly clunky, in that users see the page refresh a couple of times, but I find that having a working spinner and updating some text to indicate what is happening keeps them happy.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .