+ Start a Discussion
BellaBella 

Issues with PageReferance & Attachments

I have a really weird thing going on in my code. I need to make a pdf based on some fields from an opportunity and the opportunity owner. I click a button which directs me to a vf page where those fields are updated. Based on those updates (the most recent information) I then need to generate the final pdf and save it as an attachment to the opportunity, but what I save seems always a version behind. What I mean is, if on my intermediate page, if I select a "show lable" checkbox, the pdf in the attachment should have a label. If I uncheck it, it shouldn't. If I just call the visual force page directly, it's always up to date, always picks up the most rect information from the opportunity and owner, but when I look at the attachment, it's always a version behind, like whatever changes I'm making don't show up till next time. I have a feeling it's a problem with the PageReferance class but I don't know how else to do this. Can anyone help? Here's the skelleton of my code. This code gets called from the second button, saves the attachment, and redirects to the VF page (though really it should redirect to the atachment I just saved, but that's never up to date.)

 

 

try{
	update u;
	update o;
}catch(DmlException ex){
	ApexPages.addMessages(ex);
}

PageReference pageRef =  new PageReference('https://c.cs2.visual.force.com/apex/PDF?Id=' + o.Id);
Attachment a = new Attachment(Name = 'attachmentName.pdf', ParentId = o.Id, Description = 'attachmentDescription', Body = pageRef.getContentAsPDF());			
	
try{
	insert a;
}catch(DmlException ex){
	ApexPages.addMessages(ex);
}

String aId = a.Id;
aId = aId.substring(0, 15);
o.Generated_PDF__c = 'https://cs2.salesforce.com/servlet/servlet.FileDownload?file=' + aId;

try{
	update o;
}catch(DmlException ex){
	ApexPages.addMessages(ex);
}

return pageRef;