Recipes by Category

App Distribution (2) Bundle logic, interface and services for distribution. App Logic (37) The Apex programming language, workflow and formulas for logic. Collaboration (6) The Salesforce Chatter collaboration platform. Database (29) Data persistence, reporting and analytics. Integration (33) Web Service APIs and toolkits for integration. Security (9) Platform, application and data security. Tools (4) Force.com tooling User Interface (36) Visualforce MVC and metadata-drive user interfaces. Web Sites (12) Public web sites and apps with optional user registration and login.
Beta Feedback
Cookbook Home » Using Query String Parameters in a Visualforce Page

Using Query String Parameters in a Visualforce Page

Post by Developer Force  (2010-07-16)

Status: Certified
Level: novice

Problem

You want to read and set query string parameters in a Visualforce page, either in a custom controller or in the page itself.

Solution

The way to read and set query string parameters depends on whether you access them from a custom controller or directly from a Visualforce page.

To read a query string parameter:

  • If you're writing a custom controller, use the ApexPages global object variable and currentPage() and getParameters() methods to get query string parameters. For example, to get the value of the name query parameter in the URL: https://na1.salesforce.com/001/e?name=value, use the following line in your custom controller:
    String value = ApexPages.currentPage().getParameters().get('name');
    If you're editing a page, use the $PageContext global variable in a merge field.For example, suppose you want to add the Open Activities related list to an account detail page, but instead of showing the account's activities, you want to show the activities of a specified contact. To specify the contact, the following page looks for a query string parameter for the contact's ID under the name relatedId:
    <apex:page standardController="Account">
        <apex:pageBlock title="Hello {!$User.FirstName}!">
            You belong to the {!account.name} account.<br/>
            You're also a nice person.
        </apex:pageBlock>
        <apex:detail subject="{!account}" relatedList="false"/>
        <apex:relatedList list="OpenActivities"
                       subject="{!$CurrentPage.parameters.relatedId}"/>
    </apex:page>
    For this related list to render in a saved page, valid account and contact IDs must be specified in the URL. For example, if 001D000000HRgU6 is the account ID and 003D000000OXDIx is the contact ID, use the URL https://na3.salesforce.com/apex/MyFirstPage?id=001D000000HRgU6& relatedId=003D000000OXDIx. To set a query string parameter:If you're writing a custom controller, use the setParameters() method with ApexPages.currentPage() to add a query parameter in a test method. For example:
    String value = ApexPages.currentPage().getParameters().get('name');
    The setParameters() method is only valid inside test methods.
    If you're editing a page, you can either construct a URL manually:
    String key = 'name';
    String value = 'Caroline';
    ApexPages.currentPage().setParameters().put(key, value);
    Or you can use the <apex:param> tag as a child tag to write cleaner code:
    <apex:outputLink value="http://google.com/search?q={!account.name}">
        Search Google
    </apex:outputLink>
    In addition to <apex:outputLink>, <apex:param> can be a child of other tags such as <apex:include> and <apex:commandLink>.

Share

Recipe Activity - Please Log in to write a comment

In the last note (In addition to , can be a child of other tags such as and ). , its mentionedthat can be used in . But it is wrong. we cant use it. For refernce : https://www.salesforce.com/us/developer/docs/pages/index_Left.htm#CSHID=pages_compref_include.htm|StartTopic=Content%2Fpages_compref_include.htm|SkinName=webhelp

voted as verified by rakesh nemani  (2014-07-21)

X

Vote to Verify a Recipe

Verifying a recipe is a way to give feedback to others and broaden your own understanding of the capabilities on Force.com. When you verify a recipe, please make sure the code runs, and the functionality solves the articulated problem as expected.

Please make sure:
  • All the necessary pieces are mentioned
  • You have tested the recipe in practice
  • Have sent any suggestions for improvements to the author

Please Log in to verify a recipe

You have voted to verify this recipe.