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.