Newer Version Available

This content describes an older version of this product. View Latest

Getting and Setting Query String Parameters on a Single Page

Having seen examples of both getting and setting query string parameters, this example shows how the two actions can be combined on a single page to produce a more interesting result. Based on the example from Getting Query String Parameters, the following page makes the name of each contact in the list a hyperlink that controls the context of the detail component below it.

This is possible by:
  • Wrapping the data table in an <apex:form> tag
  • Turning each contact name into an <apex:commandLink> that sets the cid parameter appropriately with an <apex:param> tag

When used with a standard controller, command links always entirely refresh the current page with the new information added to the page—in this case, an updated cid that updates the contact detail component.

1<apex:page standardController="Account">
2    <apex:pageBlock title="Hello {!$User.FirstName}!">
3        You are displaying contacts from the {!account.name} account. 
4        Click a contact's name to view his or her details.
5    </apex:pageBlock>
6    <apex:pageBlock title="Contacts">
7        <apex:form>
8            <apex:dataTable value="{!account.Contacts}" var="contact" cellPadding="4" 
9                               border="1">
10              <apex:column>
11               <apex:facet name="header">Name</apex:facet>
12               <apex:commandLink>
13                 {!contact.Name}
14                 <apex:param name="cid" value="{!contact.id}"/>
15               </apex:commandLink> 
16              </apex:column>
17              <apex:column>
18               <apex:facet name="header">Phone</apex:facet>
19               {!contact.Phone}
20              </apex:column>
21            </apex:dataTable>
22        </apex:form>
23    </apex:pageBlock>
24    <apex:detail subject="{!$CurrentPage.parameters.cid}" relatedList="false" title="false"/>
25</apex:page>
After saving this markup, refresh your browser with the id query string parameter but without the cid parameter in the URL For example,
1https://Salesforce_instance/apex/MyFirstPage?id=001D000000IRt53
Initially the contact detail page is not rendered, but when you click a contact name the page renders the appropriate detail view.

If you use the id parameter in a URL, it must refer to the same entity referred to in the standard controller.

Note