1

I have a scenario where I have a search page for users to select and display search results. I also have a button to generate a pdf from these results.

I have a list of account in the controller which displays the search result. I am doing the exact same thing in the pdf page except I am passing the selected parameters in.

When I click on Print Button it will redirect to another VF page and again constructor is invoking.

Can any one help on this how to overcome this problem?

4
  • You can use the same controller for two pages, but you have to manage the query parameters, fields in the controller, etc. If you can post your existing code and explain your problem people may help you easier.
    – highfive
    Nov 7, 2014 at 10:21
  • If you have any sample code can you share
    – Uday
    Nov 7, 2014 at 10:28
  • You can use same controller in two VF pages. When u click on Print Button, please make sure in method (written in controller) you are not using sertRdirect(true). It would be good if you share your code so it will give us more idea what exactly you are doing. Nov 7, 2014 at 10:41
  • You can have an action attribute poinitng to a method on second VF page i.e. pdf page to access the variables set during the page1 transactions. By this u call the pdf page, the variables wont be re initialised. Most imp setRedirect should be false.
    – Pavan
    Nov 15, 2014 at 18:17

2 Answers 2

1

If your question is about "can we use a single controller instance for 2 page executions" (the non-PDF and the PDF one) then I guess the answer is no. You are forced to have a full page refresh (the content-type changes between PDF and non-PDF), and anyway you are prevented from using re-render over apex:page... So the controller has to be inited twice/contructor will run twice.

Blind guess (might be completely wrong, pls no downvote, just trying to help out)(revealed to be correct possibly):

I don't know if it's viable to use a variable as page "output format" attribute value. If so, set it as "html" by default. Then I don't know if it would work: when it's time to switch to PDF just return a PageReference without setRedirect(true) to the same page. Might be the constructor evaluation is skept/the inited values retained, and the page refreshed to PDF? Yes, seems like it can work.

6
  • 6
    It is entirely possible to use the same controller instance for 2 page executions, even when one renders as HTML and the other as PDF. A full page refresh doesn't cause the controller to be reinstantiated. As long as all pages returned share the same controller, only calling setRedirect(true) on the page reference will instantiate a new controller instance.
    – Dominic
    Nov 7, 2014 at 11:19
  • That is where I was getting in my "blind guess" - glad to see it's confirmed. The downvote is unfair! :-)
    – MLucci
    Nov 7, 2014 at 11:24
  • I downvoted when your post contained this: " I guess the answer is no. You are forced to have a full page refresh (the content-type changes between PDF and non-PDF), and anyway you are prevented from using re-render over apex:page... So the controller has to be inited twice/contructor will run twice." which is simply untrue.
    – Dominic
    Nov 7, 2014 at 11:33
  • Yes, but still I used "I guess", and still I was going in the right direction after. I'm not saying my answer was/is correct, but a downvote should be used for really bad stuff, which I think mine wasn't. IMHO. :-)
    – MLucci
    Nov 7, 2014 at 11:36
  • Anyway, I edited my answer leaving intact the try/error process I followed so that everybody will know I was ignorant before ;-)
    – MLucci
    Nov 7, 2014 at 11:42
1

Yes you can use the same controller for two different VF Pages. In order to avoid running the constructor a second time ( if you are not committing DML prior to page transfer - such as search results and a second page rendering the results ) you will need in your method to search to set

setRedirect( false );

to your page reference method that performs your search. The second page will then be able to use all of the data that you have returned here. If you are committing data on page change but using the same controller - but want to use other methods within the controller - you can set parameters in your save/search method to an object

setRedirect( true ); 

in order to commit your changes ( especially if making a callout after this change - you will need to commit changes first ).

If using method 2 - you can gather the parameters from the header in your constructor or second page action method when the second page loads and use the headers to perform your search query on page load.

For searches only i would suggest simply using the first method and redirect to the second page with

setRedirect( false ); 

on the Page reference.

NOTE: Transient variables may not persist across to the new page. so any variables that you need to persist - do not use as transient.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .