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
jason.bradleyjason.bradley 

Dynamically create VF page from another VF page's controller?

Hi,

 

I need to be able to print cards that have someone's name and whatnot else on it. I already have a VF page for inputting this information. I would like to be able to dynamically create a VF page from the controller in the original VF page that gathers the correct info, open the newly created VF page so that the user can see it while at the same time bringing up the print dialogue via a javascript call in the header. My current attempt at this from what I have managed to find online looks as follows:

 

public PageReference createCard(String name, String company, String title)
    {
        Component.Apex.Page card = new Component.Apex.Page();
        Component.Apex.Form form = new Component.Apex.Form();
        Component.Apex.OutputLabel visitorName = new Component.Apex.OutputLabel();
        Component.Apex.OutputLabel visitorCompany = new Component.Apex.OutputLabel();
        Component.Apex.OutputLabel visitorTitle = new Component.Apex.OutputLabel();

        visitorName.value='name';
        visitorName.style='text-align:center; font-size:24pt;';
        
        visitorCompany.value='name';
        visitorCompany.style='text-align:center; font-size:24pt;';
        
        visitorTitle.value='name';
        visitorTitle.style='text-align:center; font-size;24pt;';
        
        form.childComponents.add(visitorName);
        form.childComponents.add(visitorCompany);
        form.childComponents.add(visitorTitle);
        
        //Stop point
        
        return null;
    }

 

At this point I am stuck. Since the page object does not have a childComponents field, I'm not sure how to add the form to that. I am not even entirely sure that what I have done so far is right. 

Right now I was attempting to be able to return a PageReference so that the user would be sent to the newly created page and proceed from there, but with this I'm not sure how to attach any JavaScript either. Is there a better way to go about doing this?

Best Answer chosen by Admin (Salesforce Developers) 
Superdome-oldSuperdome-old
No, VF doesn't work as you think.

What you need is two separated VF pages (Surely, you can combine them together)

The first page is more like a form page to gather information from user input.

And, the second is the cards page.

You need to write the cards page and when users click 'submit' then they will go the second page.

And the simplest way, the variables are to be passed from the first page to the second using parameters.

All Answers

Superdome-oldSuperdome-old
No, VF doesn't work as you think.

What you need is two separated VF pages (Surely, you can combine them together)

The first page is more like a form page to gather information from user input.

And, the second is the cards page.

You need to write the cards page and when users click 'submit' then they will go the second page.

And the simplest way, the variables are to be passed from the first page to the second using parameters.
This was selected as the best answer
jason.bradleyjason.bradley

Thanks that helped a lot! Passing parameters to the card page is exactly the method that I need! 

After posting this I had gone ahead and just tried making a secondary page with the same controller and set it up in a way that would not create a second instance of the controller so that the variables would be transferrable between the two. This worked fine for getting a second page up with the inputted information, but then there was the issue of trying to automatically print it as a PDF, which would not work since javascript is ignored when it is rendered as a PDF. Of course then I tried to render it as a basic VF page without the toolbars and everything else, but then I could not manage to get rid of the date and time headers that are included with any printed webpage. The next thing I tried was using an iframe so that the PDF version would appear within that iframe and the main document contained javascript to automatically print the contents of that iframe when the page is loaded, but I discovered that it is not possible to use the same instance of the controller in the iframe as the containing page, so the page within the iframe just shows up blank. 

Your solution using parameters, however, sounds like exactly what I need. If I were to set up the other page to read the parameters passed in the URL as the three lines of information in the card, then it should show up correctly as well as allow it to print as soon as the page is read. I just hope that when it is rendered as a PDF it doesn't ignore those javascript calls as well.

Thanks for your help!