Activity Forums Salesforce® Discussions What is the best way to pass parameters from one visualforce page to another?

  • Vara

    Member
    March 16, 2016 at 7:26 am

    If you are using same controller for both visualforce pages then you can assign those values to variables. You can use those variables in next visualforce page. In below code we are selecting option in first page and passing it to controller and using it in second page.

    First Page and its Controller:

    `



    Select the value to pass:













    public class Sample
    {
    public String option {get;set;}

    public pageReference pass()
    {
    PageReference pg = new PageReference('/apex/SecondPage');
    pg.setRedirect(true);
    return pg;
    }
    }`

    Second Page:

    `
    The received value is {!option}
    `

  • Danna

    Member
    March 16, 2016 at 9:21 am

    There are number of way you can do it.

    1. If the value of passed parameter data is less than 4KB, which in you case would be, you can use session cookies, maybe in JSON format.

    2. You can store the data in some other object or a document, which can be retrived by the destination page.

    3. There is also an option of using Platform Cache.

    4. You can use Custom Settings Methods of the Hierarchy type. Its a little complicated, but gets the job done and get it done securely.

    5. You can use localStorage coupled with some JS that unloads the data from sending side and loads back on destination. However it has 2.5 million unicode character limit, which in turn way more than other methods, except document method that is.

    I would recommend using cookies but you should encrypt the data or else it can be a security risk. Same is the case of using documents. Custom settings and platform cache have severe space limitations, as in 1MB per licence. So you may need to use them very judiciously.

  • Karen

    Member
    March 16, 2016 at 10:09 am

    Agree... @varaprasaddhruvsoft-com

  • Parul

    Member
    September 19, 2018 at 9:04 pm

    Hi,

    When you want to pass parameters between visualforce pages use the <apex:param> tag.

    Be careful with the values you pass in the URL.. Special Characters will cause problems
    when passed in the URL.. For ex if you pass the Account Name "Shah & shah".. this would'nt
    pass properly as the special character "&" can't get into the URL..
    So as a best practice always pass ID's.. you could write a query and retrieve any other field
    in the APEX Class.

    Thanks.

  • shariq

    Member
    September 19, 2018 at 10:48 pm

    Hi,

    Try this -

    VF page 1 - 

    <apex:page docType="html-5.0" controller="mycontroller" >
    <apex:form >
    <apex:pageBlock >
    <apex:pageBlockSection>
    Date1: <apex:input type="date" value="{!date1}"/>
    Date2: <apex:input type="date" value="{!date2}"/>
    </apex:pageBlockSection>
    </apex:pageBlock>
    <apex:commandButton value="Display" action="{!display}" />
    </apex:form>
    </apex:page>

    VF page 2 - 

    <apex:page docType="html-5.0" controller="mycontroller" >
    <apex:form >
    <apex:pageBlock >
    <apex:pageBlockSection>
    Date3: <apex:input type="date" value="{!date3}"/>
    Date4: <apex:input type="date" value="{!date4}"/>
    </apex:pageBlockSection>
    </apex:pageBlock>
    </apex:form>
    </apex:page>

    Controller - 

    public class mycontroller {

    public date date1 {set;get;}
    public date date2 {set;get;}
    public date date3 {set;get;}
    public date date4 {set;get;}

    public pagereference display()
    {
    date3 = date1;
    date4 = date2;

    pagereference pr = new pagereference('/apex/page2');
    return pr;
    }

    }

    Hope this helps.

Log In to reply.

Popular Salesforce Blogs