0

I have a visualForce page with an action that download the page. I need the pdf I download to show a field of my record, the problem is that all updates I do on my record in my function that the visualForce page use as a action are not registered until the end of the function, so the pdf downloaded doesn't have the good values in it. The visualForce page is lunched by a button on the record

I will shorten my code to keep only the important part

VF page :

<apex:page standardController="WorkOrder" renderAs="pdf" applyBodyTag="false" showHeader="false" standardStylesheets="false"
       extensions="certificateDownloader" action="{!testBug}">
    <body>
        {!WorkOrder.TestBug__c}
    </body>
</apex:page>

controller:

public pageReference testBug() {
    if (ApexPages.currentPage().getParameters().get('doNotSavePDF') == null) {
        WorkOrder wo = (WorkOrder)stdController.getRecord();
        wo.TestBug__c = 'test';
        update wo;
        PageReference certificatePage = Page.testButton;
            certificatePage.getParameters().put('Id', woId);
            certificatePage.getParameters().put('doNotSavePDF', 'true');
            Blob certificatePdf = certificatePage.getContentAsPdf();
        insert new Attachment(Body = certificatePdf, Name = 'testBug', ParentId = woId);
        return stdController.view();
    }
    return null;
}

And in the pdf attached to my record, there is nothing where the TestBug__c field is supposed to be. Even though the record has been updated and the field has 'test' on it. I tried to debug the feld in the constructor, and it tells me that the field is still null on the second run.

I suspect the bug is due to the fact that The updates on a record are not really made until the end of the function in case of a error not managed that will stop the function. The only way to run the function to put what I want in the fileds to be shown in the PDF is to make the update on the controller constructor, but it is not permitted to do DML operations in the constructor.

And I have tried to create a other visualForce page that will only call the real one I want to update, but for some reason, it always gives me a blank pdf, so I can't do that.

1 Answer 1

2

This is a feature, not a bug. Using PageReference.getContent or PageReference.getContentAsPDF performs a callout to the server. This means that the code generating the PDF can't see the unsaved changes, because it is literally a completely different transaction. You would need to first save the changes to the database, then call getContentAsPDF in a separate transaction (perhaps a future method or something).

3
  • I thought doing 'update wo' was saving the changes into the database
    – Alexis
    Commented Aug 15, 2022 at 20:38
  • 1
    @Alexis to be clear, you have created a temporary save that cannot be seen outside the current transaction. Loading a page for PDF rendering occurs in a separate transaction that cannot see the original temporary save. You need to finalize the current transaction first.
    – sfdcfox
    Commented Aug 15, 2022 at 22:03
  • Ok thank you for your help!
    – Alexis
    Commented Aug 16, 2022 at 13:02

You must log in to answer this question.

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