I am attempting to get the content of a visualforce page that is rendered as a PDF. I make some changes to the object that the visualforce page is displaying data from, update that object, make the content call and then attach the attachment to the correct object. My problem is that the two fields I am updating before I make the DML call are not coming through in the getContent call. After I attach the attachment I forward the user to the visualforce page I am trying to attach. When the user views the page through visualforce the fields render correctly, it is only when attaching the page that they do not show correctly. Here is the code where I am making the content call and returning the page reference:
public PageReference submitApplication(){
application.application.Status__c = 'Submitted';
application.application.Application_Submitted_Date__c = DateTime.now();
update application.application;
update application.currentProject;
String organizationName = this.application.currentOrganization.Name;
String projectID = this.application.currentProject.id;
String currentUserId = this.application.currentUser.Id;
String currentAccId = this.application.currentOrganization.Id;
system.debug('Application submitted date: ' + application.application.Application_Submitted_Date__c);
system.debug('Application Signature: ' + application.application.Applicant_Signature__c);
PageReference applicationPage = Page.ApplicationPDF;
Blob body = applicationPage.getContent();
Attachment applicationpdf = new Attachment();
applicationpdf.Body = body;
applicationpdf.ContentType = '.pdf';
applicationpdf.Name = organizationName + ' Application PDF';
applicationpdf.Name += '.pdf';
applicationpdf.ParentId = projectID;
insert applicationpdf;
return applicationPage;
}
The application variable is just a custom controller that stores objects that the subsequent extensions use. Every single other field shows and displays correctly. It is just the 'Status__c' and 'Application_Submitted_Date__c' that are not showing up on the attached PDF. Just to reiterate - when the user actually views the returned PageReference (the same one used to generate the Content Blob) - both of those fields display the value correctly. Also the debug log prints out the values correctly before making the getContent call. I am just wondering if there is something simple that I am missing or if anyone has come across an issue like this before. Here is how the fields are displaying on the visualforce page:
<div class="formLeft">
<div class="formLeft-content">
<div class="field">
<span class="label"><apex:outputLabel value="Applicant Signature:" />
</span> <span class="value"><apex:outputField value="{!application.Applicant_Signature__c}" /></span>
</div>
</div>
</div>
<div class="formRight">
<span class="label"><apex:outputLabel value="Date Signed:" />
</span> <span class="value"><apex:outputField value="{!application.Application_Submitted_Date__c}" />
</span>
</div>
Thanks
EDIT**
Making the changes proposed by @Andrew Fawcette creates an error when trying to view the attachment that get attached. The visualforce page is set to rednder as PDF and I have also tried using the getContentAsPDF method and this attaches a white page. If I use this code outside of a future method it attaches correctly except for the issue described above. The controller for the PDF uses UserInfo information to start querying data and I am wondering if this could be causing the issue.