New Batch#100 (10th Nov 2021) - Salesforce Admin + Dev Training (WhatsApp: +91 - 8087988044) :https://t.co/p4F3oeQagK

Thursday 19 July 2012

Passing parameters between visualforce pages

Setting Parameters:
*From VF Page:
<apex:outputLink value="/apex/page4">click<apex:param name="EN" value="Success"/></apex:outputLink>
*From Page Reference:
PageReference nextpage = new PageReference('/apex/Page4?EN='+EmployeeName+'&EN2='+EmployeeName2);
-To pass more than one parameter We should use & symbol inbetween.
*  PageReference nextpage = new PageReference('/apex/Page4');
    nextpage.getParameters().put('En',EmployeeName);
    nextpage.getParameters().put('En2',EmployeeName2);
- We can use multiple put statements to pass multiple parameters.
To pass the parameters between the VF pages which have same controllers:
*No need to pass any parameters only the thing is that we should not  use nextpage.setRedirect(True);
*For this we need two VF pages and one controller.
 *VF page1:
<apex:page controller="customcontroller1">
<apex:form >
   Name<apex:inputtext id="inpEmployeeName" value="{!EmployeeName}"/><br/>
   <apex:CommandButton value="Next" action="{!next}" id="cmdNext" />
</apex:form>
</apex:page>

*VF page2:
<apex:page controller="customcontroller1">
   .The employee name is {!EmployeeName}
</apex:page>

*APEX Controller1:
public class customcontroller1 {

    public PageReference next() {
        PageReference nextpage = new PageReference('/apex/Page2');
        nextpage.setRedirect(False);//By default it is false, we should not make it true

        //if we make it true values will be vanished while we are navigating from one 
        //page to another page
        return nextpage;      
    }
    public String EmployeeName { get; set; }
}


To pass the parameters between the VF pages which have different controllers:
*For this we need two VF pages and two controllers.
*VF page1:
<apex:page controller="customcontroller1">
<apex:form >
   First<apex:inputtext id="inpEmployeeName" value="{!EmployeeName}"/><br/>
   Second<apex:inputtext id="inpEmployeeName2" value="{!EmployeeName2}"/><br/>
   Second<apex:inputtext id="inpEmployeeName3" value="{!EmployeeName3}"/><br/>
   <apex:CommandButton value="Next" action="{!next}" id="cmdNext" >     
   </apex:commandButton>
  <!-- <apex:outputLink value="/apex/page4">click<apex:param name="EN" value="{!EmployeeName}"/></apex:outputLink>-->
</apex:form>
</apex:page>
*APEX Controller1:
public class customcontroller1 {

    public String EmployeeName3 { get; set; }

    public String EmployeeName2 { get; set; }

    public PageReference next() {
        //PageReference nextpage = new PageReference('/apex/Page4?EN='+EmployeeName+'&EN2='+EmployeeName2);
        PageReference nextpage = new PageReference('/apex/Page4');
        nextpage.getParameters().put('En',EmployeeName);
        nextpage.getParameters().put('En2',EmployeeName2);
        nextpage.getParameters().put('En3',EmployeeName3);

        return nextpage.setRedirect(True); //Note that setRedirect method is used here
    }


    public String EmployeeName { get; set; }
}

*VF page2:
 <apex:page controller="customcontroller2">
   The employee name is {!EmployeeName}<br/>
   The employee name is {!EmployeeName2}<br/>
   The employee name is {!EmployeeName3}
   <!--{!$CurrentPage.parameters.EN}
   <apex:detail subject="{!$CurrentPage.parameters.EN}"/>-->
</apex:page>

*APEX Controller2:
public class customcontroller2 {

    public String EmployeeName { get; set; }
    public String EmployeeName2 { get; set; }
    public String EmployeeName3 { get; set; }
    //Constructor
  public customcontroller4()
  {
    EmployeeName = ApexPages.currentPage().getParameters().get('EN');
    EmployeeName2 = ApexPages.currentPage().getParameters().get('EN2');
    EmployeeName3 = ApexPages.currentPage().getParameters().get('EN3');

  }
}


*Use 'assignedTo' in VF page instead of using 'ApexPages.CurrentPage().getParameters().get('name')'

http://srinusfdc.blogspot.in/2012/07/actionfunction-in-vf.html

http://www.forcetree.com/2009/06/passing-parameters-to-visualforce-page.html

http://visualsalesforce.blogspot.in/2011/03/passing-values-between-pages-in.html

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hi,
    I am ok with your code but i have one confusion if you are on last page with same controller and setredirect is set to false and you want to call another method of that controller on visualforce page with commandlink then it does not work.Telll me how to call method of same controller.

    ReplyDelete
  3. what is name and value in param

    ReplyDelete

Labels