function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
VarunCVarunC 

is this Bug in getContent() PageReference function?

I've tested multiple times and found this persitent behaviour with getContent() or getContentAsPDF() methods of PageReference class.

 

Tried this:

I've a VF page which accepts a QueryString parameter "stmtid", and readsfrom an Object on page load like this:

MyObject__c[] objs = [Select Id, Name From MyObject__c Where ID =: ApexPages.currentPage().getParameters().get('stmtid')];

 

I called this VF page from a Custom Button, which works perfectly fine and generates the PDF on load, since the page is RenderAs="PDF".

 

BUT when I use this code:

 

PageReference p = Page.MyVFPDF; p.getParameters().put('stmtid', a.ID); Attachment attachment = new Attachment(); attachment.Body = p.getContentAsPDF(); // OR p.getContent() ... ... ... insert attachment;

 

 

 

Here, I getEmpty PDF file, since the Query in MyVFPDF page's extention class' contructor is returning Null value, i.e. No Record, but same ID (stmtid) value can be queried against the database and I can see the results in Eclipse Object browser ...

 

So, can anyone guide me here why I can't read the Values when I call the page in APEX calls and tries to read the Content to generate a PDF, but can use same page & stmtid variable value on a Custom Button & generate a PDF file ?

 

Best Answer chosen by Admin (Salesforce Developers) 
Rajesh ShahRajesh Shah

There lies the problem. Since the record is getting freshly created, the transaction has not yet been committed to the database even though the Id is returned. So when you call the other page, the query will return null.

I suggest that once you insert the record, you transfer control to another page and have that page do the insert of attachment. This will work because when transferring to another page, the transaction will be committed to the database.

Hope this helps.

All Answers

Rajesh ShahRajesh Shah

I had a similar requirement and I used the following code. It works for me.

 

VisualForce_URL__c url = [Select v.View_Page__c, v.Name, v.Create_Edit_Page__c From VisualForce_URL__c v where v.Name = :cse.RecordType.Name];

PageReference viewPage = new PageReference('/apex/'+url.View_Page__c+'?id=' + caseId); viewPage.getParameters().put('Action', 'Print'); printDetails = viewPage.getContent().toString();

 

 Is the stmtid parameter getting passed as null? Or the parameter has the value but the query is returning null? If it is later case, is the record with stmtid getting created just before you call the page?

 

VarunCVarunC

yes, stmtid is getting populated correctly. It is holding value when I try to read Content of VF page. I checked this with debug, but the query is not returning value.

 

Plus, thats correct assumption of yours, stmtid is being inserted a fresh before I process the page content as PDF. But, how I cross-checked that after insert of values, stmtid Do Contains an ID value.

Rajesh ShahRajesh Shah

There lies the problem. Since the record is getting freshly created, the transaction has not yet been committed to the database even though the Id is returned. So when you call the other page, the query will return null.

I suggest that once you insert the record, you transfer control to another page and have that page do the insert of attachment. This will work because when transferring to another page, the transaction will be committed to the database.

Hope this helps.

This was selected as the best answer
VarunCVarunC
yes that solution of redirecting to another page worked :). Thanks.