You need to sign in to do that
Don't have an account?
Akshata 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.
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.
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. Also tested the above solution and it is working fine.
Please do let me know if it helps you.
Regards,
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.
Please find the code which I tested in my DE environment:
FirstPage:
SecondPage:
Final Page:
Controller:
Output:
Please do let me know if it helps you.
Regards,
Mahesh