+ Start a Discussion

Question in passing data between two visualforce pages using the same controller?

I am new to development and trying to figure out the best way to pass data between pages. I have done the following right now
i) Created 2 custom objects
ii) Created 2 visualforce pages using one object each
iii) One controller class for both the pages
I get the input for 1st object in the first page and then pass them to the second page where I get the input for the 2nd object and then save both together in the second page.(No save happens in first page)

I created one custom class for both the pages but I have mentioned it as the extension class in the visualforce page.

When I navigate from Page1 to Page2, how do I pass all the data entered in the first page. I tried using ApexPages.StandardController in the constructor and then using sc.getRecord() but it throws an error because the object differs between pages. It works only when I use same object in both pages.

It looks like you need to enter the parent record fist and then child record since child record would require an ID of parent in lookup field.
It's like this:

Let's say I am entering Account, Contact and Opportunity from same UI then I need to provide all required field of Account first, then all required field of contact and then all required fields for Opportunity.

And when it's time to save the record, I would have controller behind the save button like below,
public class WizardForAccountContactsOpps {
 Account act = New Account();
    Contact con = New Contact();
    Opportunity Opp = New Opportunity();

Public PageReference SaveAll(){
    insert act;
    con.accountID = act.id;
    insert con;
    opp.accountID = act.id;
    insert Opp;
    return null;
But at the same time, I would also make sure that as user enters Account field into visualforce page and then moves on to Contact then All Account field values has been stored and not lost.

Hope this helps!

My question is how to get the data I entered on first page in the second page. I have shared the code

public with sharing class TestExtension
       public object1__c tempobject1{get;set;} 
       public TestExtension(ApexPages.StandardController sc)
              this.tempobject1=(object1__c)sc.getRecord();//this works if both page use same standard controller
       public PageReference movetoStep2()
            PageReference nextPage=new PageReference('/apex/Page2');
             return nextPage;
This works well if both of my visualforce use the same object object1__c. But my second page uses object2__C as the standard controller but this class is defined as the extension. How do I need to convert this so that I get all object1__C data in the second page. I have shared the visualforce header implementation below.

<apex:page standardController="object1__c" extensions="TestExtension" showHeader="false" standardStylesheets="false">

Page 2:
<apex:page standardController="object2__c" extensions="TestExtension" showHeader="false" standardStylesheets="false">

 I navigage from page 1 to page 2 using the movetoStep2 method. But, after arriving to page 2, I need to get the values I entered for object1 in Page1. How do I achieve this?