How do you pass parameters to a Visualforce page?

1. URL Parameters :

  • How it works : You append parameters to the end of the Visualforce page URL using a question mark (?) to start, and ampersands (&) to separate multiple parameters.

    • Example: /apex/MyPage?param1=value1&param2=value2
  • In Visualforce: Use {!$CurrentPage.parameters.paramName} to access the parameter values.


2. <apex:param> Tag :

  • How it works: Use this tag within <apex:commandLink> or <apex:commandButton> to pass parameters when navigating between Visualforce pages.

  • In Visualforce: The destination page can then retrieve these parameters using {!$CurrentPage.parameters.paramName}.


3. Apex Controller :

  • How it works: You can pass parameters to an Apex controller method through URL parameters or within <apex:actionFunction>.

  • In Apex: Use ApexPages.currentPage().getParameters().get('paramName') to retrieve the parameter values.


Important Considerations:

  • Security: Avoid passing sensitive information directly in the URL as it can be easily exposed.
  • Special Characters: URL parameters might need encoding to handle special characters correctly.
  • Data Types: Parameter values are typically treated as strings. You might need to convert them to other data types in your Apex controller.


Example: Passing parameters through URL


Visualforce Page :

<apex:page controller="MyController">
    <apex:outputLink value="/apex/AnotherPage?name=John&age=30">
        Click here to go to another page
    </apex:outputLink>
</apex:page>


Apex Controller :

public class MyController {
    // ...
}


Another Visualforce Page :

<apex:page controller="AnotherController">
    Name: {!name}
    Age: {!age}
</apex:page>


Another Apex Controller :

public class AnotherController {
    public String name {get; set;}
    public Integer age {get; set;}

    public AnotherController() {
        name = ApexPages.currentPage().getParameters().get('name');
        age = Integer.valueOf(ApexPages.currentPage().getParameters().get('age'));
    }
}

This example demonstrates how to pass parameters through the URL and retrieve them in both Visualforce and Apex.