I appear to have a very weird error.
I have a business case where I have a button on an opportunity which generates 2 records (related to that opportunity via master-detail). After those records were created, I need to generate a PDF and attach it to the opportunity. The PDF must contain data from the 2 records created.
Basically my flow is like this:
- User clicks the button
the button is linked to a visualforce page with an action in the <apex:page>
tag, like this:
<apex:page standardController="Opportunity" extensions="OpportunityActionsExtension" action="{!generateConfirmation}" />
- 2 child records are created and attached to the opportunity.
in the action in my extension I do something like this:
public PageReference generateConfirmation()
{
List<Child_record__c> children = new List<Child_record__c>();
Child_record__c child1 = new Child_record__c();
child1.Opportunity__c = this.opportunity.Id;
child1.Name = "Example 1";
children.add(child1);
Child_record__c child2 = new Child_record__c();
child2.Opportunity__c = this.opportunity.Id;
child2.Name = "Example 2";
children.add(child2);
insert children;
PageReference pdfPage = new PageReference('/apex/PdfPage');
pdfPage.getParameters().put('Id', opportunity.Id);
Attachment attachment = new Attachment();
attachment.Name = 'Confirmation.pdf';
attachment.ContentType = 'application/pdf';
attachment.ParentId = opportunity.Id;
attachment.Body = pdfPage.getContentAsPdf();
insert attachment;
return new PageReference('/' + opportunity.Id);
}
In the extension of the PdfPage, I want to display the 2 child records that were created earlier, in a list. and I have a getter like this:
public List<Child_record__c> getChildren()
{
return [SELECT Name FROM Child_record__c];
}
Now the weird thing is, in my PDF, I see outdated records.
The first time I click the button, I don't see any child records, while they were inserted in the database.
The second time I click the button, I see the 2 records in my pdf that were generated the first time I click the button.
The third time I see the 2 records from the first time, and the 2 records from the second time. For some reason I don't see my 2 newly created records, that were inserted in the same context, before the pdf is being generated.
When I put the generating of the PDF in a @future method, then it works, however I would want it to work in the same context, else the users must refresh the page. Anyone has something similar, or knows what the cause of this could be?