You need to sign in to do that
Don't have an account?

Two VF pages sharing one controller, data not available in second VF page.
Hello,
I am trying to code two VF pages that share one controller. The first page displays some data along with a selection table. The second page renders the output as a PDF page.
All works well except the display of the "selected items" table. After the user presses the "Generate RMA" button, I create a new list with all of the rows that were selected in the first page. Nothing displays for this table. All other data displays correctly.
I will upload the code for all three objects.
One observation: I have one field that I display on the second page that I don't display on the first (Claim__c.Claim_Company__c). Nothing would ever display on the second page for that field until I included it as a hidden input field on the first page. Then it worked perfectly. The table object that will not display is also a field that never appears on the first page. Could this be a similar issue?
An update:
I added these two lines in the ClaimRMAPDFTemplate:
{!selectedCDLIs[0].Claim_LI_Total_Claim_Quantity_Involved__c}
{!CDLIs[0].CDLI.Claim_LI_Total_Claim_Quantity_Involved__c}
The first returned no value, the second returned the quantity from the first row of the selection table.
This again seems to indicate that the selected-row-list (created in savePDF method, appears to be empty in the ClaimRMAPDFTemplate. I have system.debug statements that show that the list is definitely populated.
Why does the second page see no data in that list?
My whole purpose for the second list is to exclude the rows in the first list that were not selected. Is it possible to simply skip rows in the second VF page that are not selected? I was experimenting with the "rendered" attribute. I could make it work on the column component, but not on the datatable component.
Thanks.
Bryan Hunt
Bob,
I figured out how to make it work. I'm not sure *why* this logic works versus my old code... ?
I moved the logic to populate the second list into the getter for the list... and voila!!
Here is the updated controller logic:
Any thoughts on why having the code there works where it wouldn't when in the savePDF method?
Thanks.
Bryan Hunt
All Answers
First VF page:
Second VF page:
Controller:
Interesting one.
The behaviour of the Claim__c.Claim_Company__c is expected - the record that the standard controller makes available is populated via a reflection type process on the page itself, so the record only has the fields that are required for the page when the controller constructor fires. When you get to the second page, the controller is already constructed, so that doesn't have any effect on the record.
This makes it seem pretty likely that the controller is only being created once rather than twice, which is the issue I was expecting to see I must admit. Just for the sake of clarity, I notice that you have quite a bit of commented out code that was attempting to retrieve the PDF version of the page - I'm assuming you are seeing this problem with those lines commented out?
The only thing that seems slightly odd is that you have declared a getter and setter for the selected list:
but then provided another getter furhter down that is checking the side of the list.
Bob,
If I understand correctly, inclusion of fields on VF page 1 is how the standard controller determines which fields to return as part of its query (e.g. Claim__c.Claim_Company__c). So if I want to use data from the Claim record on the second page, I have to make sure that it is referenced on the first page.
Yes, it was my hope to only have one instance of the controller so that I could share the data between both pages. I believe that the "trick" to that is to use the setRedirect(false). At least that is what I gathered from another post. It seems to work quite well with the exception of the second list.
The commented out code is for what the final process actually will be: not displaying the PDF page, but rendering it as an attachment to the Claim record. It actually works perfectly, with the same caveat: I can't get the second table to render.
The two getters are actually some detritus from trying just about anything and everything I could think of to get the second list table visible. I believe that the standard getter will work just fine if I can figure this thing out.
The odd thing is that the original list is readily available, the second list is not. Not through the datatable and not through a simple field display. Yet, system.debugs show that the second list is definitely populated. Baffled....
Thanks for your assistance.
Bryan Hunt
My understanding is the same as yours for the first couple of points. The fact that you need fields in page 1 to use in page 2 very strongly suggests that it is indeed the same instance of the controller.
With regard to pulling back the PDF programmatically, you won't be able to do that as getting the content of a page as PDF takes place in a separate transaction - I've been bitten by this before.
If you have the debug in regarding the object's constructor, do you see that just the once?
Bob,
The system.debug inside the constructor does confirm (via debug logs) that it is only run once. So, same instance of controller is used by both pages.
Not sure what you mean about the PDF as attachment issue? It works perfectly.
Thanks.
Bryan Hunt
I mean that the data you enter in the first page won't be available in the PDF, as that will be generated in a separate transaction. The fixed information will be fine, but I wouldn't expect your selected information to be available. That was why I asked about it in my first post.
Bob,
I'm still confused... I selected an entry from the drop-down, then entered comments. Both appear on my PDF attachment.
Thanks.
Bryan
Without saving the record? That's not been my experience in the past. I had to save the record then go to a new page where the controller retrieved the content as an attachment.
Just to clarify - I'm talking about using the getContent method of a PageReference rather than redirecting to another page. I tried to do this for a quote PDF, but I had to save the changes to the opportunity before the getContent could pick them up.
Yes, a little different that what I'm doing here.
Both of my methods (display new page or direct to attachment) are working well for what I need.
Just gotta figure out this list access....
Thanks.
Bryan Hunt
Bob,
I figured out how to make it work. I'm not sure *why* this logic works versus my old code... ?
I moved the logic to populate the second list into the getter for the list... and voila!!
Here is the updated controller logic:
Any thoughts on why having the code there works where it wouldn't when in the savePDF method?
Thanks.
Bryan Hunt
Glad to hear you got there. I really can't think why it would be working one way and not the other. It almost smacks of some viewstate race condition, where the setter is called with the original empty list after the action method has completed and set up the revised list. That doesn't match the order of execution though, so I'm still baffled.
Hi,
I have a similar requirement. How are you storing the selectedList ?
Because for me, in order to get the selectedList, I need to write a pageReference method and hence it is not a property having getter setters. Therefore, I am getting a blank selected list on next page.
Please advice me.
Thanks.
I need how did you implemet, onclick or action method for your checkBox for selection.
I am able to see the selectedList
I am getting empty list on my second VF PDF page attachement.
Currently we are taking the old values from contructor and incrementing by 1 and displaying which works all fine but in case if simultaneouly there are two invoices generated the problem arises.
Please any could help as i have been trying to sort it out more than a month now but all the options i tried didnt give me the desired result
<apex:page standardController="Invoice__c" extensions="GSTorginal" showHeader="false" applyHtmlTag="false" renderAs="pdf" applyBodyTag="false">
<tr><apex:panelGroup rendered="{!taxa!='GST'}"><td height="50%" colspan="4"><h>To</h><br/><br/>
{!acc.BillingStreet}<br/>{!acc.BillingCity}<br/>{!acc.BillingState} {!acc.BillingPostalCode}<br/>{!acc.BillingCountry} {!acc.BillingLatitude} {!acc.BillingLongitude}
</td>
</apex:panelGroup>
</apex:panelGroup><td height="50%" colspan="5">
<apex:panelGroup rendered="{!acc.Name !='PricewaterHouse Coopers Pvt Ltd'}">
<h align="right" style="display:{!if(flag=='attach','','none')}">
Invoice No : 1--{!inm1.name}<br/></h>
<h align="right" style="display:{!if(flag=='false','','none')}">
Invoice No : 2---{!inm1.name }<br/></h>
<h align="right" style="display:{!if(flag=='print','','none')}">
Invoice No : 3-- {!inm1 } <br/></h>
<h align="right" style="display:{!if(flag=='view','','none')}">
Invoice No : IE-00{!invNo+1} <br/></h>
</apex:panelGroup>
<apex:panelGroup rendered="{!acc.Name =='PricewaterHouse Coopers Pvt Ltd'}">
<h align="right" style="display:{!if(flag=='attach','','none')}">
Invoice No : {!inm1}-<apex:outputText value="{0, date,yyyy}">
<apex:param value="{!TODAY()}" /></apex:outputText> <br/></h>
<h align="right" style="display:{!if(flag=='false','','none')}">
Invoice No : IE-00{!invNo }-<apex:outputText value="{0, date,yyyy}">
<apex:param value="{!TODAY()}" /></apex:outputText> <br/></h>
<h align="right" style="display:{!if(flag=='print','','none')}">
Invoice No : {!inm1 }-<apex:outputText value="{0, date,yyyy}">
<apex:param value="{!TODAY()}" /></apex:outputText> <br/></h>
<h align="right" style="display:{!if(flag=='view','','none')}">
Invoice No : IE-00{!invNo+1}-<apex:outputText value="{0, date,yyyy}">
<apex:param value="{!TODAY()}" /></apex:outputText> <br/></h>
</apex:panelGroup>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</body>
</html>
</apex:page>
appointment1 = new SCSCHAMPS__Appointment__c();
appde= new SCSCHAMPS__Appointment__c();
invBill=new Invoice__c();
cb=false;
invfp = new Invoice__c();
totall = 0;
contactList = new List<ContactWrapper>();
AppList = new List<AppWrap>();
addMore();
selectedRowIndex = '0';
flag='false';
system.debug('**appointment1.SCSCHAMPS__Employer__c**'+appointment1.SCSCHAMPS__Employer__c);
system.debug('---Initial Flag'+flag);
inv = [select id,name,invoicetext__c from invoice__C order by name desc limit 1];
String s1= inv.Name;
invNo = (Long)Long.valueOf( s1.substring(3));
}
Pagereference ref;
//
Some code to generate invoice
//
attachPdfToAccount( invBill.Billing_Calendar__c, appointment.SCSCHAMPS__Employer__c,inv.id);
flag = 'attach';
return ref;
}
public void attachPdfToAccount(String invBillCalendar, String appointmentEmployer, String invId){
try{
if(ep=='None'&& desp!=null)
{
this.ep=desp;
}
else{
this.ep=ep;
}
Billing_Calendar__c bc=new Billing_Calendar__c();
bc=[select id,Name, Invoicing_Start_Date__c,Invoicing_End_Date__c from Billing_Calendar__c where Billing_Calendar__c.id=:invBillCalendar limit 1];
Account acc = [select id,Name from Account where ID=:appointmentEmployer];
Attachment myAttach1 = new Attachment();
flag = 'attach';
myAttach1.ParentId = invId;
inm1= [SELECT id,Name,Invoice_Date__c,invoicetext__C FROM Invoice__c Where Id =: invId];
inm1.invoicetext__C=inm1.name;
update inm1;
myAttach1.name =inm1.invoicetext__C+'.pdf';
pagereference ref=page.FAGSTVFPORGINAL;
ref.getParameters().put('inm1',String.ValueOf(inm1.invoicetext__C));
ref.getParameters().put('inm1',String.ValueOf(inm1.Name));
ref.getParameters().put('inm1',inm11);
ref.getParameters().put('inm1',inm12);
ref.getParameters().put('id',invid);
ref.getParameters().put('accId',acc.Id);
ref.getParameters().put('flag1',String.valueOf(flag));
ref.getParameters().put('ep1',ep);
ref.getParameters().put('toi1',toi);
ref.getParameters().put('stax1',stax);
ref.getParameters().put('scurr1',scurr);
ref.getParameters().put('pos',pos);
ref.getParameters().put('sop1',sop);
ref.getParameters().put('ids',strids);
//GST Implementation
ref.getParameters().put('taxa1',taxa);
ref.getParameters().put('addG1',addG);
ref.getParameters().put('gst1',gst);
ref.getParameters().put('startDate',string.valueOf(bc.Invoicing_Start_Date__c));
ref.getParameters().put('endDate',string.valueOf(bc.Invoicing_End_Date__c));
ref.getParameters().put('invt',invoicetext);
if(edate!=null)
ref.getParameters().put('edt',string.valueOf(edate));
else
ref.getParameters().put('edt',string.valueOf(Date.Today()));
if(bdate!=null)
ref.getParameters().put('bdt',string.valueOf(bdate));
else
ref.getParameters().put('bdt',string.valueOf(Date.Today()));
ref.setRedirect(false);
//myAttach1.body = ref.getContentAsPDF();
ref.getParameters().put('flag1','print');
Blob body;
try {
// returns the output of the page as a PDF
body = ref.getContent();
// need to pass unit test -- current bug
} catch (VisualforceException e) {
body = Blob.valueOf('Some Text');
}
myAttach1.body=body;
myAttach1.IsPrivate = false;
insert myAttach1;
list<attachment> alist=[select id,name from attachment where id=:inm1.id];
inm5=[SELECT Id, Name, invoicetext__c,(SELECT Id, Name FROM Attachments) FROM invoice__c order by name desc limit 1];
ref.getParameters().put('inm5',string.valueOf(inm5));
}
catch(Exception e) {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL,'There was an error while attaching'));
System.debug('Exception');
}
}