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
Akshata Asukar 10Akshata Asukar 10 

Maintaing the data during redirections

Hi I am currently working on a requirement, which requires the user to enter details in VF page 1 click next and enter details on VF page 2, enter details and again click next, then perform the insertion of record on clicking a button in VF page 3. All the pages use the same controller. Can any one help me how to accomplish this. My code is pasted below.

Controller
public class RedirectionController
{
    public Marchendise__c mar{get;set;}
    public integer num{get;set;}
    public RedirectionController()
    {
    }
    
    public PageReference NextPage()
    {
    System.debug('+++debug+++'+mar);
        PageReference pg=new PageReference('/apex/SecondPage');
        pg.setRedirect(false);
        return pg;
    }
    
     public PageReference NextPage2()
    {
    System.debug('+++debug2+++'+mar);
    System.debug('++num++'+num);
        PageReference pg=new PageReference('/apex/FinalPage');
        pg.setRedirect(false);
        return pg;
    }
    public void Save()
    {
    insert mar;
    }
}

PAGE 1
<apex:page controller="RedirectionController">
  <apex:form>
  <apex:pageBlock>
  <apex:inputField value="{!mar.Line_Item__c}"/>
  <apex:pageBlockButtons title="Next" >
  <apex:commandButton value="Next" action="{!NextPage}">
  </apex:commandButton>
  </apex:pageBlockButtons>
  </apex:pageBlock>
  </apex:form>
</apex:page>

PAGE 2
<apex:page controller="RedirectionController">
 <apex:form>
  <apex:pageBlock>
  <apex:inputField value="{!mar.Price__c}"/>
  <apex:pageBlockButtons title="Next" >
  <apex:commandButton value="Next" action="{!NextPage2}"/>
  </apex:pageBlockButtons>
  </apex:pageBlock>
  </apex:form>
</apex:page>

PAGE 3
<apex:page controller="RedirectionController">
  <apex:form>
  <apex:pageBlock>
  <apex:inputField value="{!mar.Quantity__c}"/>
  <apex:pageBlockButtons title="Next" >
  <apex:commandButton value="Next" action="{!Save}"/>
  </apex:pageBlockButtons>
  </apex:pageBlock>
  </apex:form>
</apex:page>

In the end I get error as Attempt to dereference null object because there is no data in mar instance of Marchendise__c.

 
Mahesh DMahesh D
Hi Akshata,

Please find the below code:

Here I included the initialization in the constructor. And also Name field is a required field and we have to populate it before saving the record.
public class RedirectionController
{
    public Marchendise__c mar{get;set;}
    public integer num{get;set;}
    public RedirectionController()
    {
        mar = new Marchendise__c();
        mar.Name = 'Test Mar';
    }
    
    public PageReference NextPage()
    {
    System.debug('+++debug+++'+mar);
        PageReference pg=new PageReference('/apex/SecondPage');
        pg.setRedirect(false);
        return pg;
        
    }
    
     public PageReference NextPage2()
    {
    System.debug('+++debug2+++'+mar);
    System.debug('++num++'+num);
        PageReference pg=new PageReference('/apex/FinalPage');
        pg.setRedirect(false);
        return pg;
    }
    public PageReference Save()
    {
        insert mar;
        return new PageReference('/'+mar.Id);
    }
}
Also tested the above solution and it is working fine.

Please do let me know if it helps you.

Regards,
Mahesh​

 
Akshata Asukar 10Akshata Asukar 10
Hi Mahesh,

After initialising mar the record is getting saved now, but the values assigned in the first and second page are lost in third page and the record is created with only the Quantity__c field being populated.
Mahesh DMahesh D
Hi Akshata,

Please find the code which I tested in my DE environment:

FirstPage:

 
<apex:page controller="RedirectionController">
    <apex:form >
        <apex:pageBlock>
            <apex:pageBlockSection>
                <apex:inputField value="{!mar.Line_Item__c}"/>
            </apex:pageBlockSection>
            <apex:pageBlockButtons title="Next" >
                <apex:commandButton value="Next" action="{!NextPage}">
                </apex:commandButton>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

SecondPage:
 
<apex:page controller="RedirectionController">
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockButtons>
                <apex:inputField value="{!mar.Price__c}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockButtons title="Next" >
            <apex:commandButton value="Next" action="{!NextPage2}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Final Page:
 
<apex:page controller="RedirectionController">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection>
                <apex:inputField value="{!mar.Quantity__c}"/>
            </apex:pageBlockSection>
            <apex:pageBlockButtons title="Next" >
                <apex:commandButton value="Next" action="{!Save}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
 
public class RedirectionController {
    public Marchendise__c mar{get;set;}
    public integer num{get;set;}
    
    public RedirectionController() {
        mar = new Marchendise__c();
        mar.Name = 'Test Mar';
    }
    
    public PageReference NextPage() {
        System.debug('+++debug+++'+mar);
        PageReference pg=new PageReference('/apex/SecondPage');
        pg.setRedirect(false);
        return pg;
    }
    
    public PageReference NextPage2() {
        System.debug('+++debug2+++'+mar);
        System.debug('++num++'+num);
        PageReference pg=new PageReference('/apex/FinalPage');
        pg.setRedirect(false);
        return pg;
    }
    
    public PageReference Save() {
        insert mar;
        return new PageReference('/'+mar.Id);
    }
}

Output:

User-added image

User-added image

User-added image

User-added image


Please do let me know if it helps you.

Regards,
Mahesh